1 linked list€¦ · //3 queue using doubly linked list #include #include struct node{//node of...

42
//1 linked list #include <stdio.h> #include <stdlib.h> struct node{//node of linked list int val;//value part struct node *addr;//address of next node }; struct node *start;//holds address of first node, initially NULL int empty()//is list empty { if(start==NULL) return 1; else return 0; } void traverse()//travel from first node to last node in linked list { struct node *cur = start; printf("List: "); while(cur!=NULL) { printf("%d ",cur->val); cur = cur->addr; } printf("\n"); } void ins(int x)//insert a new element in linked list { struct node *temp, *cur, *prev; cur = start; prev = NULL; temp = (struct node *)malloc(sizeof(struct node));//create a new node temp->val = x;//assign value temp->addr = NULL;//make address part as NULL if(start==NULL)//if first node { start=temp; } else//find place for it in sorted linked list { while(cur!=NULL && cur->val <= x) {

Transcript of 1 linked list€¦ · //3 queue using doubly linked list #include #include struct node{//node of...

Page 1: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//1 linked list #include <stdio.h> #include <stdlib.h> struct node{//node of linked list int val;//value part struct node *addr;//address of next node }; struct node *start;//holds address of first node, initially NULL int empty()//is list empty { if(start==NULL) return 1; else return 0; } void traverse()//travel from first node to last node in linked list { struct node *cur = start; printf("List: "); while(cur!=NULL) { printf("%d ",cur->val); cur = cur->addr; } printf("\n"); } void ins(int x)//insert a new element in linked list { struct node *temp, *cur, *prev; cur = start; prev = NULL; temp = (struct node *)malloc(sizeof(struct node));//create a new node temp->val = x;//assign value temp->addr = NULL;//make address part as NULL if(start==NULL)//if first node { start=temp; } else//find place for it in sorted linked list { while(cur!=NULL && cur->val <= x) {

Page 2: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

prev = cur; cur = cur->addr; } temp->addr = cur; prev->addr = temp; } traverse(); } void del(int x)//delete a node if present in linked list { struct node *cur, *prev; cur = start; prev = NULL; if(empty())//no element in linked list { printf("Error : list is empty"); } else//atleast 1 element { while(cur!=NULL && cur->val==x)//find a node having value=x from first node to last node { prev = cur; cur = cur->addr; } if(cur==NULL)//not found printf("Error : not found"); else//found, delete it { if(prev==NULL)//first element start = cur->addr; else prev->addr = cur->addr;//non first element traverse(); } } } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 20 30 ins(10);//10 10 20 30 ins(50);//10 10 20 30 50 del(20);//10 10 30 50 del(70);//error : not found del(10);//10 30 50

Page 3: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

del(10);//30 50 del(50);//30 del(30);// del(40);//error : empty getch(); }

Page 4: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//2 stack using linked list (doubly) #include <stdio.h> #include <stdlib.h> struct node{//node of linked list int val;//value part struct node *prev;//address of prev node struct node *next;//address of next node }; struct node *top;//holds address of stack top, initially NULL int empty()//is stack empty { if(top==NULL) return 1; else return 0; } void traverse()//travel from first node to last node in linked list { struct node *x = top; printf("stack:\n"); while(x!=NULL) { printf("%d\n",x->val); x = x->prev; } printf("\n"); } void push(int x)//insert a new element in stack at stack top { struct node *temp; temp = (struct node *)malloc(sizeof(struct node));//create a new node temp->val = x;//assign value temp->prev = NULL;//make address part as NULL temp->next = NULL; if(top==NULL)//if first node { top=temp; } else//find place for it in sorted linked list { top->next = temp;//insert new element on top of stack temp->prev = top;

Page 5: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

top = temp;//new element is new stack top } traverse(); } void pop()//remove top most element { struct node *x; if(empty())//no element in stack { printf("Error : stack is empty"); } else//atleast 1 element { if(top->prev!=NULL) (top->prev)->next = NULL; top = top->prev; traverse(); } } int main() { push(10);//10 push(30);//10 30 push(20);//10 30 20 push(10);//10 30 20 10 push(50);//10 30 20 10 50 pop();//10 30 20 10 pop();//10 30 20 pop();//10 30 pop();//10 pop();// pop();//error : stack empty getch(); }

Page 6: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//3 queue using doubly linked list #include <stdio.h> #include <stdlib.h> struct node{//node of queue (FIFO) int val;//value part struct node *next, *prev;//address of next node and prev node }; //tail-from where new node can be inserted and head-from where existing node can be deleted struct node *head, *tail; int empty()//to find queue is empty or not { if(head==NULL) return 1; else return 0; } void show() { struct node *temp; temp = head; printf("Queue : "); if(!empty()) { temp = head; while(temp != NULL) { printf("%d ",temp->val); temp = temp->next; } } printf("\n"); } void ins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp; }

Page 7: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

else { tail->next = temp; tail = temp; } show(); } void del() { if(empty()) { printf("Error : queue is empty\n"); } else { head = head->next; if(head!=NULL) head->prev = NULL; show(); } } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 30 20 ins(10);//10 30 20 10 del();//30 20 10 del();//20 10 del();//10 del();// del();//error : empty getch(); }

Page 8: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//4 convert infix to postfix using doubly linked list #include<stdio.h> #include<stdlib.h> #include<string.h> struct node{ char ch; struct node *next, *prev; }; int empty(); void push(char); char pop(); struct node *top; char in[10];//to read infix exp int main() { int l,i; printf("Enter infix expression:"); gets(in); l = strlen(in); for(i=0; i<l; i++) { switch(in[i]) { case '+': while( (!empty()) && (top->ch=='/' || top->ch=='*' || top->ch=='^') ) { printf("%c",pop()); } push(in[i]); break; case '-': while( (!empty()) && (top->ch=='/' || top->ch=='*' || top->ch=='^') ) { printf("%c",pop()); } push(in[i]); break; case '.': while(!empty()) { printf("%c",pop()); } break; case '(': push('('); break;

Page 9: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

case ')': while(top->ch!='(')//pop & print till stack top != opening brace { printf("%c",pop()); } pop();//pop opening brace break; case '*': while( (!empty()) && (top->ch=='/' || top->ch=='*' || top->ch=='^') ) { printf("%c",pop()); } push(in[i]); break; case '/': while( (!empty()) && (top->ch=='/' || top->ch=='^') ) { printf("%c",pop()); } push(in[i]); break; case '^': while( (!empty()) && (top->ch=='^') ) { printf("%c",pop()); } push(in[i]); break; default: printf("%c",in[i]);//operand } } } int empty() { if(top==NULL) return 1; else return 0; } void push(char x) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->ch=x; temp->next=NULL; temp->prev=NULL; if(!empty())

Page 10: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

{ top->next=temp; temp->prev=top; } top = temp; } char pop() { char x; if(!empty()) { x =top->ch; top = top->prev; if(top!=NULL) top->next=NULL; return x; } else { return NULL; } }

Page 11: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//5a input restricted dequeue using doubly linked list #include <stdio.h> #include <stdlib.h> struct node{//node of queue (FIFO) int val;//value part struct node *next, *prev;//address of next node and prev node }; //tail-from where new node can be inserted and head-from where existing node can be deleted //tail end will also be used to delete an existing element struct node *head, *tail; int empty()//to find queue is empty or not { if(head==NULL) return 1; else return 0; } void show() { struct node *temp; temp = head; printf("Queue : "); if(!empty()) { temp = head; while(temp != NULL) { printf("%d ",temp->val); temp = temp->next; } } printf("\n"); } void ins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp;

Page 12: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

} else { temp->prev = tail; tail->next = temp; tail = temp; } show(); } void hdel()//deleting a node from head end { if(empty()) { printf("Error : queue is empty\n"); } else { head = head->next; if(head!=NULL) head->prev = NULL; show(); } } void tdel()//deleting a node from tail end { if(empty()) { printf("Error : queue is empty\n"); } else { tail = tail->prev; if(tail!=NULL) tail->next = NULL; show(); } } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 30 20 ins(10);//10 30 20 10 hdel();//30 20 10

Page 13: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

tdel();//30 20 tdel();//30 hdel();// tdel();//error : empty getch(); }

Page 14: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//5b output restricted dequeue using doubly linked list #include <stdio.h> #include <stdlib.h> struct node{//node of queue (FIFO) int val;//value part struct node *next, *prev;//address of next node and prev node }; //tail-from where new node can be inserted and head-from where existing node can be deleted //head end can be used to insert a new element too struct node *head, *tail; int empty()//to find queue is empty or not { if(head==NULL) return 1; else return 0; } void show() { struct node *temp; temp = head; printf("Queue : "); if(!empty()) { temp = head; while(temp != NULL) { printf("%d ",temp->val); temp = temp->next; } } printf("\n"); } void tins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp;

Page 15: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

} else { temp->prev = tail; tail->next = temp; tail = temp; } show(); } void hins(int val)//to insert new element from tail end in queue { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = val; temp->next = NULL; temp->prev = NULL; if(empty()) { head = temp; tail = temp; } else { temp->next = head; head->prev = temp; head = temp; } show(); } void del()//deleting a node from head end { if(empty()) { printf("Error : queue is empty\n"); } else { head = head->next; if(head!=NULL) head->prev = NULL; show(); } } int main() { tins(10);//10

Page 16: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

hins(30);//30 10 tins(20);//30 10 20 hins(10);//10 30 10 20 del();//30 10 20 del();//10 20 del();//20 del();// del();//error : empty getch(); }

Page 17: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//7 expression tree of postfix expression of maximum 50chars including EOE #include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 50 struct node{ char ch; struct node *left, *right; }; struct node *stack[SIZE]; int top=0; struct node *root; void push(struct node *temp) { if(top!=SIZE)//if stack is not full { stack[top++]=temp; } else { printf("Error: stack full\n"); } } struct node * create(char val) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->ch=val; temp->left=NULL; temp->right=NULL; push(temp); return temp; } struct node * pop() { struct node *addr; if(top!=0) { addr = stack[top-1]; top--; return addr; }

Page 18: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

else { printf("Error: stack empty"); return; } } void show(struct node *temp) { if(temp!=NULL) { printf("%c ",temp->ch); show(temp->left); show(temp->right); } } int main() { char s[50]; int i; struct node *op1, *op2,*temp; printf("Enter postfix expression:"); gets(s); for(i=0; i<strlen(s); i++) { switch(s[i]) { case '*': case '-': case '/': case '+': case '^': op1=pop(); op2=pop(); temp = create(s[i]); temp->left=op2; temp->right=op1; break; case '.':show(pop()); break; default:temp=create(s[i]); break; } } }

Page 19: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//8 AVL tree insertion #include<stdio.h> #include<stdlib.h> struct node { int key; struct node *left, *right; int height; }; struct node *root; struct node * newNode(int val) { struct node *temp = (struct node *)malloc(sizeof(struct node)); temp->key=val; temp->left=NULL; temp->right=NULL; temp->height=1; return temp; } int max(int a, int b) { return (a>b)?a:b; } int getHeight(struct node *temp) { if(temp==NULL) return 0; else return temp->height; } int balance(struct node *temp) { if(temp==NULL) return 0; else return ( getHeight(temp->left)-getHeight(temp->right) ); } struct node * rightRotate(struct node *y) { struct node *x = y->left; struct node *z = x->right;

Page 20: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//rotation x->right = y; y->left = z; //update heights y->height = max( getHeight(y->left),getHeight(y->right))+1; x->height = max( getHeight(x->left),getHeight(x->right))+1; //return new root return x; } struct node * leftRotate(struct node *y) { struct node *x = y->right; struct node *z = x->left; //rotation x->left = y; y->right = z; //update heights y->height = max( getHeight(y->left),getHeight(y->right))+1; x->height = max( getHeight(x->left),getHeight(x->right))+1; //return new root return x; } struct node * insert(struct node *x, int val) { int bal; //if AVL tree is empty then current node is root if(x==NULL) return( newNode(val) ); //recursive call to insert() as new node has to be attached as leaf node if( x->key>val )//insert in left subtree x->left = insert(x->left,val); else//insert in right subtree x->right = insert(x->right,val); //update height of ancestor //new height of ancestor will be eqaul to max height among left & right child, +1 //due to recursive call of insert() height of all ancestor will updated x->height = max( getHeight(x->left), getHeight(x->right) )+1; //check balance of this ancestor to find out if it is unbalanced or not

Page 21: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

bal = balance(x); //if unbalanced then can fall in one of the following 4 cases //LL case if(bal>1 && val<(x->left)->key) return rightRotate(x); //RR case if(bal<-1 && val>(x->right)->key) return leftRotate(x); //LR case if(bal>1 && val>(x->left)->key) { x->left = leftRotate(x->left); return rightRotate(x); } //RL case if(bal<-1 && val<(x->right)->key) { x->right = rightRotate(x->right); return leftRotate(x); } //if no unbalance then return x; } void preorder(struct node *temp)//preorder traversal on tree { if(temp!=NULL) { printf("%d ",temp->key); preorder(temp->left); preorder(temp->right); } } int main() { root = NULL; root = insert(root,10); root = insert(root,20); root = insert(root,30); root = insert(root,40); root = insert(root,50); root = insert(root,25); preorder(root); }

Page 22: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//9a quick sort #include<stdio.h> int data[10] = {10,9,8,7,6,5,4,3,2,1};//worst case void show() { int i; for(i=0; i<10; i++) printf("%d ",data[i]); printf("\n"); } int part(int lo, int hi) { int pivot = data[lo]; int i=lo+1; int j=hi; int t; while(1) { while(data[j]>pivot){ j--; } while(data[i]<pivot){ i++; } if(i<j) { t = data[i]; data[i]=data[j]; data[j]=t; } else { t = data[j]; data[j]=data[lo]; data[lo]=t; return j; } } } void quickSort(int lo, int hi) { int p; if(lo<hi) {

Page 23: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

p = part(lo,hi); quickSort(lo,p); quickSort(p+1,hi); } } int main() { quickSort(0,9); show(); return 0; }

Page 24: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//9b merge sort #include<stdio.h> int data[10] = {10,9,8,7,6,5,4,3,2,1};//worst case int temp[10]; void show() { int i; for(i=0; i<10; i++) printf("%d ",data[i]); printf("\n"); } void merge(int lo, int mid, int hi) { int x=lo; int y=mid+1; int i; for(i=lo; x<=mid && y<=hi; i++) { if(data[x]<=data[y]) temp[i]=data[x++]; else temp[i]=data[y++]; } while(x<=mid) temp[i++]=data[x++]; while(y<=hi) temp[i++]=data[y++]; for(i=lo; i<=hi; i++) data[i] = temp[i]; } void part(int lo, int hi) { int mid; if(lo<hi) { mid = (lo+hi)/2; part(lo,mid); part(mid+1,hi); merge(lo,mid,hi); } else

Page 25: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

return; } int main() { part(0,9); show(); return 0; }

Page 26: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

/*6 heap tree del() can be used to create 9c Heap sort

max heap hence descending sort tree using an array (indexing from 0) if a node is at n index then its parent at ceil((n-1)/2) left child at 2n+1 right child at 2n+2 binary tree with m level will have max 2^m-1 nodes */ #include<stdio.h> #include<math.h> #define LEVEL 3 int heap[7]; int next=0;//next position to insert a new element int size; void show() { int i; printf("sorted data: "); for(i=0;i<next;i++) printf("%d ",heap[i]); } void heapify(int n) { int temp; if(n==0)//root does not have parent to compare return; int parent = (int)floor((double)(n-1)/2); if(heap[parent]>heap[n]) return; else { temp = heap[parent]; heap[parent]=heap[n]; heap[n]=temp; heapify(parent); } } void heapifyDown(int n) { int temp; if(n>next)//can not go further last node return; int left = 2*n+1;//location of left child

Page 27: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

int right = 2*n+2;//location of right child if( (left<next && heap[left]<heap[n]) && (right<next && heap[right]<heap[n]) )//index does exist && child value is less than parent return;//no further heapify necessary else if(left<next && heap[left]>=heap[right] && heap[left]>heap[n])//left child > parent && left child > right child && left does exist { temp = heap[left];//swap heap[left]=heap[n]; heap[n]=temp; heapifyDown(left);//heapify further down } else if(right<next && heap[right]>=heap[left] && heap[right]>heap[n])//right child > parent && right child > left child && right does exist { temp = heap[right];//swap heap[right]=heap[n]; heap[n]=temp; heapifyDown(right);//heapify further down } } void del() { int temp; next--; printf("%d ",heap[0]);//print root value temp=heap[0];//swap last element with root heap[0]=heap[next]; heap[next]=temp; heapifyDown(0);//heapify in down direction from root to leaf } int main() { int i; size=(int)pow(2,LEVEL)-1;//max nodes that a binary tree can have for(i=0;i<size;i++)//take a data and create/update heap tree { /*printf("Enter value:"); scanf("%d",&heap[next]);*/ heap[next]=i+1; heapify(next); next++; } show();//show heap tree printf("\nSorted data:"); for(i=0;i<size;i++)//delete each node one by one, gives data in heap sorted del();

Page 28: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

return 0; }

Page 29: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//9d insertion sort #include<stdio.h> int data[10] = {10,9,8,7,6,5,4,3,2,1};//worst case void show() { int i; for(i=0; i<10; i++) printf("%d ",data[i]); printf("\n"); } void insertSort() { int i,j,t; for(i=1; i<=9; i++) { for(j=i; j>0 && data[j]<data[j-1]; j--) { t = data[j]; data[j]=data[j-1]; data[j-1]=t; } } } int main() { insertSort(); show(); return 0; }

Page 30: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//10a graph: bfs //assumed to be connected graph (directed and undirected anyone) //if not connected then nay get stuck #include<stdio.h> int gm[4][4]={ {0,1,0,1}, {1,0,1,0}, {0,1,0,0}, {1,0,0,0} }; char names[4]={'a','b','c','d'}; int visited[4]={0,0,0,0};//0-unvisited & unknown, 1-unvisited & known, 2-visited & known int q[4];//bfs Q of unvisted vertices int top=0;//point to start of Q void addQ(int x) { visited[x]=1; q[top++]=x; } int Qempty() { if(top==0) return 1; else return 0; } void Qremove() { int i; for(i=0;i<top;i++) q[i]=q[i+1]; top--; } void bfs(int x) { int i; printf("%c ",names[x]);//print in BFS path visited[x]=2;//declare as visited & known for(i=0;i<4;i++)//add all unknown neighbors in Q { if(visited[i]==0 && gm[x][i]!=0) addQ(i);

Page 31: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

} Qremove();//remove from Q } int main() { int start; printf("BFS path: "); start=0; addQ(start); while(!Qempty()) { bfs(q[0]); } return 0; }

Page 32: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//10b graph: dfs //assumed to be connected graph (directed and undirected anyone) //if not connected then nay get stuck #include<stdio.h> int gm[4][4]={ {0,1,0,1}, {1,0,1,0}, {0,1,0,0}, {1,0,0,0} }; char names[4]={'a','b','c','d'}; int visited[4]={0,0,0,0};//0-unvisited & unknown, 1-unvisited & known, 2-visited & known int stack[4];//dfs stack of unvisted vertices int top=0;//point to start of stack void push(int x) { visited[x]=1; stack[top++]=x; } int empty() { if(top==0) return 1; else return 0; } void pop() { top--; } void dfs(int x) { int i; push(x); for(i=0; i<4; i++) { if(visited[i]==0 && gm[x][i]!=0) dfs(i); } printf("%c ",names[x]); pop(); }

Page 33: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

int main() { int start; printf("DFS path: "); start=0; do { dfs(start); }while(!empty()); return 0; }

Page 34: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//11 static list, collection of elements in sorted order #include<stdio.h> #include<stdlib.h> #define SIZE 5 int list[SIZE];//list using an array of fixed size int next=0;//special pointer indicating location to store new element or delete existing int full()//is list full? { if(next==SIZE) return 1; else return 0; } int empty()//is list empty? { if(next==0) return 1; else return 0; } void disp()//show all elements in list { int i; printf("Static List: "); for(i=0; i<next; i++) printf("%d ",list[i]); printf("\n"); } void ins(int x)//insert a new value { int i, j; if(full())//error if list is full { printf("Error : List is full\n"); } else//if not then insert new element in sorted order { for(i=0; i<next && list[i]<x; i++);//find place for new element in list for(j=next; j>i; j--)//shift right list[j] = list[j-1]; list[i] = x; next++; disp();

Page 35: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

} } void del(int x)//delete existing value { int i, j; if(empty())//error if list is empty { printf("Error : List is empty"); } else { j = search(x); if(j==next) printf("Error : element to delete not found\n"); else { for(i=j; i<next-1; i++)//left shift list[i] = list[i+1]; next--; disp(); } } } int search(int s)//search if an element is present or not { int i; for(i=0; i<next; i++) { if(list[i]==s)//element found return i; } return i;//element not found } int main() { ins(10);//10 ins(30);//10 30 ins(20);//10 20 30 ins(10);//10 10 20 30 ins(50);//10 10 20 30 50 ins(60);//error : full del(20);//10 10 30 50 del(70);//error : not found del(10);//10 30 50 del(10);//30 50

Page 36: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

del(50);//30 del(30);// del(40);//error : empty getch(); }

Page 37: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

//12 stack using an array #include<stdio.h> #include<stdlib.h> #define SIZE 3 int stack[SIZE];//stack in array int top=0;//stack top, index of last element int full() { if(top==SIZE) return 1; else return 0; } int empty() { if(top==0) return 1; else return 0; } void show() { int i; if(empty()) printf("Stack : Empty"); else { for(i=0; i<top; i++) printf("%d ",stack[i]); } printf("\n"); } void push(int val) { if(full()) { printf("Error: stack full\n"); } else { stack[top++] = val; show(); }

Page 38: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

} void pop() { int i; if(empty()) { printf("Error: stack empty\n"); } else { top--; show(); } } int main() { push(10);//10 push(20);//10 20 push(30);//10 20 30 push(40);//error: stack full pop();//10 20 pop();//10 pop();// pop();//error: stack empty return 0; }

Page 39: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

// 13 Binary Search Tree where every node has unique value (if same then whichever comes first will be deleted first)

#include<stdio.h> #include<stdlib.h> struct node { int val; struct node *parent, *left, *right; }; struct node *root; void show(struct node *temp)//DFS, postorder traversal { if(root==NULL) printf("Empty\n"); if(temp==NULL) return; show(temp->left); show(temp->right); printf("%d ",temp->val); } void showAddr(struct node *temp)//DFS, postorder traversal { if(temp==NULL) return; showAddr(temp->left); showAddr(temp->right); printf("%d~%d\n",temp->val,temp); } void ins(int x) { struct node *temp, *cur, *prev; temp = (struct node *)malloc(sizeof(struct node)); temp->val = x; temp->parent = NULL; temp->left = NULL; temp->right = NULL; if(root == NULL) { root = temp; } else {

Page 40: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

prev = NULL; cur = root; while(cur != NULL) { prev = cur; if(x <= cur->val) cur = cur->left; else cur = cur->right; } temp->parent = prev; if(x<=prev->val) prev->left = temp; else prev->right = temp; } show(root); printf("\n"); } struct node * bst(int x) { struct node *cur; cur = root; while( cur!=NULL && cur->val!=x )//search till found or end of tree { if( x<=cur->val ) cur = cur->left; else cur = cur->right; } //printf("\n~%d~\n",cur); return cur;//if NULL then not found else address of that node*/ } void del(int x) { int newValue; struct node *temp; struct node *next; temp = bst(x); if(temp==NULL) printf("\n%d not found in BST\n",x); else//non root node { if(temp->right==NULL && temp->left==NULL)//leaf node { if(temp==root)//root node is to be deleted root = NULL;

Page 41: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

else { if((temp->parent)->left == temp )//left leaf child of parent (temp->parent)->left = NULL; else (temp->parent)->right = NULL;//right leaf child of parent } } else if(temp->right==NULL)//with only left child { if(temp==root)//root node is to be deleted root = temp->left; else { if(( temp->parent)->left == temp )//left non leaf child of parent { (temp->parent)->left = temp->left; (temp->left)->parent = temp->parent; } else//right non leaf child of parent { (temp->parent)->right = temp->left; (temp->left)->parent = temp->parent; } } } else if(temp->left==NULL)//with only right child { if(temp==root)//root node is to be deleted root = temp->right; else { if(( temp->parent)->left == temp )//left non leaf child of parent { (temp->parent)->left = temp->right; (temp->right)->parent = temp->parent; } else//right non leaf child of parent { (temp->parent)->right = temp->right; (temp->right)->parent = temp->parent; } } } else//node with two childern { //find inorder successor of node to be deleted (min in right subtree) next = temp->right;

Page 42: 1 linked list€¦ · //3 queue using doubly linked list #include  #include  struct node{//node of queue (FIFO) int val;//value part struct node *next,

while(next->left!=NULL) next = next->left; newValue = next->val;//temporary store value del(next->val);//delete the inorder successor temp->val = newValue; } printf("\n"); } } int main() { ins(10);//10 ins(5);//5 10 ins(7);//7 5 10 ins(4);//4 7 5 10 ins(12);//4 7 5 12 10 ins(11);//4 7 5 11 12 10 del(7); show(root);//4 5 11 12 10 del(12); show(root);//4 5 11 10 del(5); show(root);//4 11 10 del(10); show(root);//4 11 del(11); show(root);//4 del(5);//not found show(root);//4 del(4); show(root);//empty return 0; }