#include<stdio.h> #define STACKSIZE 20 //stack size is 20 int stack[STACKSIZE]={0}; //top of the stack initially -1 which means empty int tos=-1; void push(int ele) { if(tos>=STACKSIZE) { printf("err: Stack is full\n"); } else { tos++; stack[tos] = ele; } } int pop() { if(tos==-1) { printf("err: stack is empty\n"); } else { int temp = stack[tos]; tos--; printf("poped element from the stack is %d\n",temp); return temp; } } void display() { int i; for(i=0;i<=tos;i++) printf(" %d\n",stack[i]); if(tos== -1) { printf("err: stack is empty\n"); return; } } int main() { int ch,ele; printf("enter the choice 1-push 2-pop 3-display 9-exit\n"); scanf("%d",&ch); while(ch!=9) { switch(ch) { case 1: printf("Enter the ele to push\n"); scanf("%d",&ele); push(ele); break; case 2: ele = pop(); break; case 3: display(); break; case 4: break; default: break; } printf("enter the choice 1-push 2-pop 3-display 9-exit\n"); scanf("%d",&ch); } }
Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts
Sunday, July 28, 2013
Stack implementation in C
Monday, November 5, 2012
C Program for Rearranging linked list first odd next even numbers
Below is the C program for rearranging the linked list (not copying the elements), in such a way that, first odd elements next even elements should come as shown below. I faced this question in Amazon Interview first round. So Sharing the program here.
Input list: 1 2 3 4 5 6 7 8 9 10
OutputList: 1 3 5 7 9 2 4 6 8 10
Input Linked List:
1->2->3->4->5->6->7->8->9->10->11->12->13->14->15->16->17->18->19
Resulted List:
1->3->5->7->9->11->13->15->17->19->2->4->6->8->10->12->14->16->18
Input list: 1 2 3 4 5 6 7 8 9 10
OutputList: 1 3 5 7 9 2 4 6 8 10
#include<stdio.h> #include<stdlib.h> //linked list structure struct node { int info; struct node *next; }; //making typdef so we can use Node instead of 'struct node' typedef struct node Node; //rearranging the linked list in such a way that odd no.s first //and then even no. Node* oddEven(Node* h) { Node* tempNode = h; Node* oddEnd = NULL,*oddHead = NULL; Node* evenHead = NULL, *evenEnd = NULL; while( tempNode != NULL) { if(!(tempNode->info%2)) { if(evenHead == NULL) evenHead = tempNode; else evenEnd->next = tempNode; evenEnd = tempNode; } else { if(oddHead == NULL) oddHead = tempNode; else oddEnd->next = tempNode; oddEnd = tempNode; } tempNode = tempNode->next; } h = oddHead; oddEnd->next = evenHead; evenEnd->next = NULL; return h; } //inserting node or creating the list or adding the element @ end Node* insert(Node *h, int info) { Node *tempHead=h; Node *newNode = (Node*)malloc(sizeof(Node)); newNode->info = info; newNode->next = NULL; if(tempHead == NULL) // if the list has zero elements. make new node as a head { h=newNode; } else if(tempHead->next==NULL) // if the list is having only one node { tempHead->next = newNode; } else { Node *tempNode = h; while(tempNode->next != NULL) // if the list has more than one node, so moving to the last node { tempNode = tempNode->next; } tempNode->next = newNode; // appending the new node at the end } return h; } /***************************************************************************** for displaying the nodes in the list *****************************************************************************/ void display(Node *h) { Node *temp = h; while (temp->next!=NULL) { printf("%d->",temp->info); temp = temp->next; } printf("%d\n",temp->info); } int main() { Node *head = NULL; int i,n,element,choice,pos,size; for (i=1;i<20;i++) { head = insert(head,i); } display(head); head=oddEven(head); display(head); }Output:
Input Linked List:
1->2->3->4->5->6->7->8->9->10->11->12->13->14->15->16->17->18->19
Resulted List:
1->3->5->7->9->11->13->15->17->19->2->4->6->8->10->12->14->16->18
Tuesday, October 30, 2012
implementing doubly linked list using single pointer
The
problem with single linked list is that, we cant back traverse, only forward
traversal is allowed because current node contains the address of the next
node.To traverse back wards we need
another pointer to store the previous address and that concept is called doubly
linked list.
We
can implement back traversal by storing previous and next node address in the
single pointer. For this we need to use the concept of XOR operation.
XOR
Operations: let’s see the beauty of XOR operation.
If you perform XOR operation on any two input values, you will get some result. And if you perform again XOR operation with result and any one of the two input values, you will get other input value. We are going to use this logic to store the prev and next node values. See the example below.
a ^ b = c
c ^ b = a
c ^ a = b
So
while creating the linked list, we need to perform XOR operation to get the
next node value.The key point in this
process is, we need to maintain the prev node value to make the XOR operation.
Creating
and storing the next pointer value:The
value which we are going to store in the next pointer is the XOR operation of
prev and new nodes. So next pointer of the node should not points to the next
node, instead of that, its having some value which is a combination of prev and
next nodes as shown in the picture. For complete code click here.
If
the list contains single node, as prev pointer value is initially NULL, so
there wont be any problem.
Forward traversing: to move the pointer from current node to next node, the
sample code is given below. For complete C implementation click here.
current
= current->next ^ prev
Before Operation:
After Operation:
After executing above statement, the current node moves to the next node as shown below.
Backward traversal: Assume that below is
the scenario and we are at current node and we know the address of temp, so to
get the prev node of the current node, the sample code is below. For complete C code click here.
while(temp!=h) { printf("%d->",temp->info); prev = prev->next ^ temp; temp = tempPrev; tempPrev = prev; }
Before
operation:
In
this way, weneed to do the operations
to get the prev node and next node addresses. And there are more statements to
maintain prev and next node pointers. Click here for the complete working C Implementation.
Note:
XOR operation can be performed only on integer or char data type. And XOR is
not allowed on structure as it is user defined data type. To perform the XOR
operation, we need to type cast it to long int.
Subscribe to:
Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...