UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

Size: px
Start display at page:

Download "UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru."

Transcription

1 UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. 1

2 Table of Contents 1. Array Implementation of Linked List Queue using Array Implementation of Linked List Linked list implementation with basic operations Linked list implementation with a getnode( ) function Linked List based implementation of queue Linked list implementation of queue with local front & rear pointer Linked list implementation of stack with local pointer to the stack and double pointer Stack with local pointer & typedef for node pointer Stack using Circular Linked list Circular Queue using Circular Linked list Doubly Linked List...43

3 1. Array Implementation of Linked List. // Array Implementation of Linked List. #include<stdio.h> #include<stdlib.h> #define NUMNODES 100 struct nodetype int info; int next; ; struct nodetype node[numnodes]; int avail = -1; int list = -1; // Initialization of available nodes' list (free pool of nodes) void initavaillist() avail = 0; int i; for(i=avail; i<numnodes-1;i++) node[i].next = i + 1; node[numnodes-1].next = -1; // Marks end of avail list // return the index of a free node in the array int getnode() int p; if(avail == -1) printf("overflow\n"); return -1; p = avail; avail = node[avail].next; return p; // Freeing & adding the node to avail list void freenode(int p)

4 node[p].next = avail; avail = p; void insertafter(int p, int x) int q; if(p == -1) printf("void insertion\n"); q = getnode(); node[q].info = x; node[q].next = node[p].next; node[p].next = q; void delafter(int p, int *px) int q; if( (p==-1) (node[p].next == -1)) printf("void deletion\n"); q = node[p].next; *px = node[q].info; node[p].next = node[q].next; freenode(q); void insertbegin(int x) int p = getnode(); if(p!= -1) if(list ==-1) // first node node[p].next = -1; node[p].next = list;

5 node[p].info = x; list = p; int deletebegin() if(list == -1) printf("list empty\n"); return -1000; int p = list; int x = node[p].info; list = node[list].next; freenode(p); return x; void display() int p = list; printf("the List:"); while(p!= -1) printf("node[%d] = %d\t", p,node[p].info ); p = node[p].next; printf("\n"); int main() int x; initavaillist(); printf("insertbegin(10)\n"); insertbegin(10); display(); printf("insertbegin(20)\n"); insertbegin(20); display(); printf("insertbegin(30)\n"); insertbegin(30);

6 display(); printf("deletebegin()\n"); deletebegin(); display(); printf("insertbegin(40)\n"); insertbegin(40); display(); printf("insertafter(1,50)\n"); insertafter(1,50); display(); printf("delafter(1,&x)\n"); delafter(1,&x); display(); printf("\ndeleted %d",x); return 0;

7 2. Queue using Array Implementation of Linked List #include<stdio.h> #include<stdlib.h> #define NUMNODES 100 struct queue // stores index of first and last element of queue int front; int rear; ; struct nodetype int info; int next; ; struct nodetype node[numnodes]; // global queue container int avail = -1; // Initialising the available list with all nodes of the array void initavaillist() avail = 0; int i; for(i=avail; i<numnodes-1;i++) node[i].next = i + 1; node[numnodes-1].next = -1; // Marks end of AvailList // return the index of first free node in avail list int getnode() int p; if(avail == -1) printf("overflow\n");

8 return -1; p = avail; avail = node[avail].next; return p; // Add the node to avail list void freenode(int p) node[p].next = avail; avail = p; void insert(struct queue *pq, int x) // Insert at rear of queue int p = getnode(); node[p].info = x; node[p].next = -1; if(pq->rear == -1) // Check if Queue is empty pq->front = p; node[pq->rear].next = p; pq->rear = p; int empty(struct queue *pq) return ((pq->front == -1)?1:0); int delete(struct queue *pq) // delete from front of queue int p,x; if(empty(pq)) printf("queue Underflow\n"); return -1; p = pq->front; x = node[p].info;

9 pq->front = node[p].next; if(pq->front == -1) pq->rear = -1; freenode(p); return x; void display(struct queue *pq) int p = pq->front; printf("the Queue:"); while(p!= -1) printf("%d ", node[p].info ); p = node[p].next; printf("\n"); int main() struct queue q; // global q front & rear markers q.front = -1; q.rear = -1; initavaillist(); printf("insert(10)\n"); insert(&q,10); display(&q); printf("insert(20)\n"); insert(&q,20); display(&q); printf("insert(30)\n"); insert(&q,30); display(&q); printf("delete()\n"); delete(&q);

10 display(&q); return 0;

11 3. Linked list implementation with basic operations. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node *next; ; struct node *list = NULL; // list is global to starting of the list void insert_at_start(int newdata); // Insert new node at start of the list. void delete_first_node(); // delete first node of list void insert_at_end(int newdata); // Insert new node at end of list void delete_last_node(); // delete last node of list int delete_element(int elem); // delete node with given element. void insert_after(int existingdata, int newdata); // Insert new node after given element value void insert_at_start(int newdata) // Creation of new node struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; // Store data temp->data = newdata; if (list== NULL) printf("first node\n"); list=temp; list->next=null; temp->next=list; list=temp; void delete_first_node() struct node *temp; temp=list; if(list!= NULL)

12 list = list->next; free(temp); printf("list is already empty\n"); // Element not found void insert_at_end(int num) // Create new node struct node *cur; if(list == NULL) insert_at_start(num); cur = list; // Novigate to end of the list while(cur->next!= NULL) cur=cur->next; // Create new node struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; // Insert temp->data=num; cur->next =temp; temp->next = NULL; void delete_last_node() struct node *prev = NULL,*cur; cur=list; if(list!= NULL) // Navigate to last node while(cur->next!= NULL) prev = cur; cur = cur->next; if(prev == NULL) printf("only one node in list. Deleting it to result in empty list");

13 free(cur); list = NULL; free(cur); prev->next = NULL; printf("list is already empty\n"); int delete_element(int num) struct node *cur, *prev; cur = list; // cur points to first node initially. while(cur!=null) if(cur->data==num) if(cur==list) list=cur->next; free(cur); return 1; prev->next=cur->next; free(cur); return 1; // Success prev=cur; cur= cur->next; return 0; // Element not found void insert_after(int val, int newdata) struct node *cur; cur = list; while(cur!=null) if(cur->data == val)

14 cur=cur->next; if(cur!=null) // Create new node struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; // insert temp->data=newdata; temp->next = cur->next; cur->next = temp; printf("element not found!!"); void display() struct node* temp=list; if(temp==null) printf("list is Empty\n"); printf("the List is : "); while(temp!=null) printf("%d-->",temp->data); temp=temp->next; printf("null\n"); // Count number of nodes in the list int count() struct node *temp; int c=0; temp=list; while(temp!=null) temp=temp->next; c++; return c;

15 int main() int i,num,elem; list=null; while(1) printf("\nlist Operations\n"); printf("===============\n"); printf("1.insert At Start\t"); printf("2.insert At End\t\t"); printf("3.insert_after given element\n"); printf("4.delete At Start\t"); printf("5.delete At End\t\t"); printf("6.delete Given element\n"); printf("7.display\t\t"); printf("8.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&i)<=0) printf("enter only an Integer\n"); exit(0); switch(i) case 1: printf("enter the number to insert : "); scanf("%d",&num); insert_at_start(num); display(); case 2: printf("enter the number to insert : "); scanf("%d",&num); insert_at_end(num); display(); case 3: printf("enter the number to insert :"); scanf("%d",&num); printf("enter the existing elem :"); scanf("%d",&elem); insert_after(elem,num); display(); case 4: delete_first_node(num); display(); case 5: delete_last_node(num);

16 display(); case 6: if(list==null) printf("list is Empty\n"); printf("enter the number to delete : "); scanf("%d",&num); if(delete_element(num)) printf("%d deleted successfully\n",num); printf("%d not found in the list\n",num); display(); case 7: display(); case 8: return 0; default: printf("invalid option\n"); return 0;

17 4. Linked list implementation with a getnode( ) function. // Linked list implementation with a getnode function. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node *next; ; struct node *list = NULL; // list is global to starting of the list struct node* getnode(); // Creates a new empty node and return the address void insert_at_start(int newdata); // Insert new node at start of the list. void insert_at_end(int newdata); // Insert new node at end of list void insert_after(int existingdata, int newdata); // Insert new node after given element value int delete_element(int elem); // delete node with given element. void delete_first_node(); // delete first node of list void delete_last_node(); // delete last node of list struct node* search(int x); // Search for an element & return the pointer of the node struct node* getnode() struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; return temp; void insert_at_start(int newdata) // Creation on new node struct node *temp; temp = getnode(); // Store data temp->data=newdata; if (list== NULL) printf("first node\n"); list=temp; list->next=null;

18 temp->next=list; list=temp; void delete_first_node() struct node *temp; temp=list; if(list!= NULL) list = list->next; free(temp); printf("list is already empty\n"); // Element not found void insert_at_end(int num) // Create new node struct node *cur; if(list == NULL) insert_at_start(num); cur = list; // Novigate to end of the list while(cur->next!= NULL) cur=cur->next; // Creation on new node struct node *temp; temp = getnode(); // Insert temp->data=num; cur->next =temp; temp->next = NULL; void delete_last_node() struct node *prev = NULL,*cur; cur=list;

19 if(list!= NULL) // Navigate to last node while(cur->next!= NULL) prev = cur; cur = cur->next; if(prev == NULL) printf("only one node in list. Deleting it to result in empty list.\n"); free(cur); list = NULL; free(cur); prev->next = NULL; printf("list is already empty\n"); int delete_element(int num) struct node *cur, *prev = NULL; cur=list; while(cur!=null) if(cur->data==num) if(cur==list) // deleting first node itself. So list need to be updated list=cur->next; free(cur); return 1; prev->next=cur->next; free(cur); return 1; // Success prev=cur; cur= cur->next;

20 return 0; // Element not found void insert_after(int val, int newdata) // place struct node *cur; cur = list; while(cur!=null) if(cur->data == val) cur=cur->next; if(cur!=null) // Create new node // Creation on new node struct node *temp; temp = getnode(); // insert temp->data=newdata; temp->next = cur->next; cur->next = temp; printf("element not found!!"); struct node* search(int x) struct node *cur; cur = list; while(cur!=null) if(cur->data == x) cur=cur->next; return cur;

21 void display() struct node* temp=list; if(temp==null) printf("list is Empty\n"); printf("the List is : "); while(temp!=null) printf("%d-->",temp->data); temp=temp->next; printf("null\n"); // Count number of nodes in the list int count() struct node *temp; int c=0; temp=list; while(temp!=null) temp=temp->next; c++; return c; int main() int i,num,elem; list=null; while(1) printf("\nlist Operations\n"); printf("===============\n"); printf("1.insert At Start\t"); printf("2.insert At End\t\t"); printf("3.insert_after given element\n"); printf("4.delete At Start\t"); printf("5.delete At End\t\t"); printf("6.delete Given element\n"); printf("7.search\t\t"); printf("8.display\t\t"); printf("9.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&i)<=0) printf("enter only an Integer\n");

22 exit(0); switch(i) case 1: printf("enter the number to insert : "); scanf("%d",&num); insert_at_start(num); display(); case 2: printf("enter the number to insert : "); scanf("%d",&num); insert_at_end(num); display(); case 3: printf("enter the number to insert :"); scanf("%d",&num); printf("enter the existing elem :"); scanf("%d",&elem); insert_after(elem,num); display(); case 4: delete_first_node(num); display(); case 5: delete_last_node(num); display(); case 6: if(list==null) printf("list is Empty\n"); printf("enter the number to delete : "); scanf("%d",&num); if(delete_element(num)) printf("%d deleted successfully\n",num); printf("%d not found in the list\n",num); display(); case 7: printf("enter the element to search :"); scanf("%d",&elem); struct node *res = search(elem); if(res == NULL)

23 printf("element not found in list\n"); printf("element found in list at node %p\n",res); case 8: display(); case 9: return 0; default: printf("invalid option\n"); return 0;

24 5. Linked List based implementation of queue. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node *next; ; struct node *front = NULL; // Pointer to front of Q. NULL for empty Q struct node *rear = NULL; // Pointer to rear of Q struct node* getnode(); // Creates a new empty node and return the address void freenode(struct node* n); struct node* getnode() struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; return temp; void freenode(struct node* n) free(n); //Insert at rear end of Q void insert(int num) // Create new node // Creation & init of new node struct node *temp; temp = getnode(); temp->data=num; temp->next = NULL; if(rear == NULL) front = rear = temp; rear->next = temp; rear =temp;

25 // remove from front end Q void delete() struct node *temp; temp = front; if(front!= NULL) front = front->next; freenode(temp); if(front == NULL) rear = NULL; // List is empty printf("list is already empty\n"); // Element not found void display() struct node* temp=front; if(temp==null) printf("list is Empty\n"); printf("the List is : "); while(temp!=null) printf("%d-->",temp->data); temp=temp->next; printf("null\n"); int main() int choice,num; front = NULL; while(1) printf("\nlist Operations\n");

26 printf("===============\n"); printf("1.insert At End\t\t"); printf("2.delete At Start\t"); printf("3.display\t\t"); printf("4.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); switch(choice) case 1: printf("enter the number to insert : "); scanf("%d",&num); insert(num); display(); case 2: delete(num); display(); case 3: display(); case 4: return 0; default: printf("invalid option\n"); return 0;

27 6. Linked list implementation of queue with local front & rear pointer. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node *next; ; struct Queue struct node *front; // list is global to starting of the list struct node *rear; ; struct node* getnode(); // Creates a new empty node and return the address void freenode(struct node* n); void insert(struct Queue* q, int newdata); // Insert new node at end of list void delete(struct Queue* q); // delete last node of list struct node* getnode() struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; return temp; void freenode(struct node* n) free(n); void insert(struct Queue* q, int num) // Create new node // Creation & init of new node struct node *temp; temp = getnode(); temp->data=num; temp->next = NULL;

28 if(q->rear == NULL) q->front = q->rear = temp; q->rear->next = temp; q->rear =temp; void delete(struct Queue* q) struct node *temp; temp = q->front; if(q->front!= NULL) q->front = q->front->next; freenode(temp); if(q->front == NULL) q->rear = NULL; // List is empty printf("list is already empty\n"); // Element not found void display(struct Queue *q) struct node* temp=q->front; if(temp==null) printf("list is Empty\n"); printf("the List is : "); while(temp!=null) printf("%d-->",temp->data); temp=temp->next; printf("null\n");

29 int main() int choice,num; struct Queue q; q.front = NULL; q.rear = NULL; while(1) printf("\nlist Operations\n"); printf("===============\n"); printf("1.insert At End\t\t"); printf("2.delete At Start\t"); printf("3.display\t\t"); printf("4.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); switch(choice) case 1: printf("enter the number to insert : "); scanf("%d",&num); insert(&q,num); display(&q); case 2: delete(&q); display(&q); case 3: display(&q); case 4: return 0; default: printf("invalid option\n"); return 0;

30 7. Linked list implementation of stack with local pointer to the stack and double pointer. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node *next; ; struct node *getnode(); // Creates a new empty node and return the address void freenode(struct node *n); void push(struct node **st, int newdata); // Insert new node at start void pop(struct node **st); // delete from start struct node *getnode() struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->next = NULL; return temp; void freenode(struct node *n) free(n); void push(struct node **st, int newdata) // Create new node // Creation & init of new node struct node *temp; temp = getnode(); temp->data= newdata; temp->next = NULL; if(*st == NULL) // First push printf("first Push\n"); *st = temp;

31 printf("subsequent Push\n"); temp->next = *st ; *st = temp; void pop(struct node **st) struct node *temp; temp = *st; if(*st!= NULL) *st = (*st)->next; freenode(temp); printf("stack is already empty\n"); // Element not found void display(struct node *st) struct node *temp=st; if(temp==null) printf("stack is Empty\n"); printf("the Stack is : "); while(temp!=null) printf("%d-->",temp->data); temp=temp->next; printf("null\n"); int main() int choice,num; struct node *st = NULL; while(1)

32 printf("\nstack Operations\n"); printf("===============\n"); printf("1.push\t\t"); printf("2.pop\t"); printf("3.display\t\t"); printf("4.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); switch(choice) case 1: printf("enter the number to push : "); scanf("%d",&num); push(&st,num); display(st); case 2: pop(&st); display(st); case 3: display(st); case 4: return 0; default: printf("invalid option\n"); return 0;

33 8. Stack with local pointer & typedef for node pointer. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node * next; ; typedef struct node * NODEPTR; NODEPTR getnode(); // Creates a new empty node and return the address void freenode(nodeptr n); void push(nodeptr *st, int newdata); // Insert new node at start void pop(nodeptr *st); // delete from start NODEPTR getnode() NODEPTR temp; temp=(nodeptr )malloc(sizeof(struct node)); temp->next = NULL; return temp; void freenode(nodeptr n) free(n); void push(nodeptr *st, int newdata) // Create new node // Creation & init of new node NODEPTR temp; temp = getnode(); temp->data=newdata; temp->next = NULL; if(*st == NULL) // First push printf("first Push\n"); *st = temp;

34 printf("subseq Push\n"); temp->next = *st ; *st = temp; void pop(nodeptr *st) NODEPTR temp; temp = *st; if(*st!= NULL) *st = (*st)->next; freenode(temp); printf("stack is already empty\n"); void display(nodeptr st) NODEPTR temp=st; if(temp==null) printf("stack is Empty\n"); printf("the Stack is : "); while(temp!=null) printf("%d-->",temp->data); temp=temp->next; printf("null\n"); int main() int choice,num; NODEPTR st = NULL; while(1)

35 printf("\nstack Operations\n"); printf("===============\n"); printf("1.push\t\t"); printf("2.pop\t"); printf("3.display\t\t"); printf("4.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); switch(choice) case 1: printf("enter the number to push : "); scanf("%d",&num); push(&st,num); display(st); case 2: pop(&st); display(st); case 3: display(st); case 4: return 0; default: printf("invalid option\n"); return 0;

36 9. Stack using Circular Linked list. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node * next; ; typedef struct node * NODEPTR; NODEPTR getnode(); // Creates a new empty node and return the address void freenode(nodeptr n); void push(nodeptr *st, int newdata); // Insert new node at start int pop(nodeptr *st); // delete from start NODEPTR getnode() NODEPTR temp; temp=(nodeptr )malloc(sizeof(struct node)); temp->next = NULL; return temp; void freenode(nodeptr n) free(n); int empty(nodeptr *pstack) return (*pstack == NULL)?1:0; void push(nodeptr *pstack, int newdata) // Create new node // Creation & init of new node NODEPTR p; p = getnode(); p->data=newdata; p->next = NULL; if(*pstack == NULL) // First push

37 printf("first Push\n"); *pstack = p; p->next = *pstack; printf("subseq Push\n"); p->next = (*pstack)->next ; (*pstack)->next = p; int pop(nodeptr *pstack) int x; NODEPTR p; if(empty(pstack)== 1) printf("stack is already empty\n"); return -1000; p = (*pstack)->next; x = p->data; if(p == *pstack) // Only one node in stack *pstack = NULL; (*pstack)->next = p->next; freenode(p); return x; void display(nodeptr st) if(st==null) printf("stack is Empty\n"); NODEPTR temp=st->next;

38 printf("the Stack is :\n"); do printf("%d-->",temp->data); temp=temp->next; while(temp!=st->next); printf("start\n"); int main() int choice,num; NODEPTR st = NULL; while(1) printf("\nstack Operations\n"); printf("===============\n"); printf("1.push\t\t"); printf("2.pop\t"); printf("3.display\t\t"); printf("4.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); switch(choice) case 1: printf("enter the number to push : "); scanf("%d",&num); push(&st,num); display(st); case 2: printf("removed element %d\n",pop(&st)); display(st); case 3: display(st); case 4: return 0; default: printf("invalid option\n"); return 0;

39

40 10. Circular Queue using Circular Linked list. #include<stdio.h> #include<stdlib.h> // node definition struct node int data; struct node * next; ; typedef struct node * NODEPTR; NODEPTR getnode(); // Creates a new empty node and return the address void freenode(nodeptr n); void insert(nodeptr *st, int newdata); // Insert new node at start int delete(nodeptr *st); // delete from start NODEPTR getnode() NODEPTR temp; temp=(nodeptr )malloc(sizeof(struct node)); temp->next = NULL; return temp; void freenode(nodeptr n) free(n); int empty(nodeptr *cq) return (*cq == NULL)?1:0; void insert(nodeptr *cq, int newdata) // Create new node // Creation & init of new node NODEPTR p; p = getnode(); p->data=newdata; p->next = NULL; if(*cq == NULL) // First insert

41 printf("first insert\n"); *cq = p; p->next = *cq; printf("subseq insert\n"); p->next = (*cq)->next ; (*cq)->next = p; *cq = p; int delete(nodeptr *cq) int x; NODEPTR p; if(empty(cq)== 1) printf("stack is already empty\n"); return -1000; p = (*cq)->next; x = p->data; if(p == *cq) // Only one node in stack *cq = NULL; (*cq)->next = p->next; freenode(p); return x; void display(nodeptr st) if(st==null) printf("queue is Empty\n"); NODEPTR temp=st->next;

42 printf("the Queue is :\n"); do printf("%d-->",temp->data); temp=temp->next; while(temp!=st->next); printf("start\n"); int main() int choice,num; NODEPTR st = NULL; while(1) printf("\nstack Operations\n"); printf("===============\n"); printf("1.insert\t\t"); printf("2.delete\t"); printf("3.display\t\t"); printf("4.exit\n\n"); printf("enter your choice : "); if(scanf("%d",&choice)<=0) printf("enter only an Integer\n"); exit(0); switch(choice) case 1: printf("enter the number to insert : "); scanf("%d",&num); insert(&st,num); display(st); case 2: printf("removed element %d\n",delete(&st)); display(st); case 3: display(st); case 4: return 0; default: printf("invalid option\n"); return 0;

43 11. Doubly Linked List // A C program to demonstrate insertion & deletion in DLL #include <stdio.h> #include <stdlib.h> // A linked list node struct Node int data; struct Node *right; struct Node *left; ; /* Given a reference (pointer to pointer) to the head of a list and an int, inserts a new node on the front of the list. */ void insert_begin(struct Node** head, int new_data) /* 1. allocate node */ struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); /* 2. put in the data */ new_node->data = new_data; /* 3. Make right of new node as head and leftious as NULL */ new_node->right = (*head); new_node->left = NULL; /* 4. change left of head node to new node */ if((*head)!= NULL) (*head)->left = new_node ; /* 5. move the head to point to the new node */ (*head) = new_node; /* Given a node as left_node, insert a new node after the given node */ void insertafter(struct Node* left_node, int new_data) /*1. check if the given left_node is NULL */ if (left_node == NULL) printf("the given leftious node cannot be NULL"); /* 2. allocate new node */ struct Node* new_node =(struct Node*) malloc(sizeof(struct Node)); /* 3. put in the data */ new_node->data = new_data; /* 4. Make right of new node as right of left_node */ new_node->right = left_node->right;

44 /* 5. Make the right of left_node as new_node */ left_node->right = new_node; /* 6. Make left_node as leftious of new_node */ new_node->left = left_node; /* 7. Change leftious of new_node's right node */ if (new_node->right!= NULL) new_node->right->left = new_node; /* Given a reference (pointer to pointer) to the head of a DLL and an int, insert_ends a new node at the end */ void insert_end(struct Node** head, int new_data) /* 1. allocate node */ struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); struct Node *last = *head; /* used in step 5*/ /* 2. put in the data */ new_node->data = new_data; /* 3. This new node is going to be the last node, so make right of it as NULL*/ new_node->right = NULL; /* 4. If the Linked List is empty, then make the new node as head */ if (*head == NULL) new_node->left = NULL; *head = new_node; /* 5. Else traverse till the last node */ while (last->right!= NULL) last = last->right; /* 6. Change the right of last node */ last->right = new_node; /* 7. Make last node as leftious of new node */ new_node->left = last; // This function prints contents of linked list starting from the given node void printlist(struct Node *node) printf("\ntraversal in forward direction \n");

45 printf("null <==> "); while (node!= NULL) printf(" %d <==>", node->data); node = node->right; printf("null\n"); /* Function to delete a node in a Doubly Linked List. head_ref --> pointer to head node pointer. del --> pointer to node to be deleted. */ void deletenode(struct Node **head_ref, struct Node *del) /* base case */ if(*head_ref == NULL del == NULL) /* If node to be deleted is head node */ if(*head_ref == del) *head_ref = del->right; /* Change right only if node to be deleted is NOT the last node */ if(del->right!= NULL) del->right->left = del->left; /* Change left only if node to be deleted is NOT the first node */ if(del->left!= NULL) del->left->right = del->right; /* Finally, free the memory occupied by del*/ free(del); /* Driver program to test above functions*/ int main() /* Start with the empty list */ struct Node* head = NULL; // Insert 6. So linked list becomes 6->NULL insert_end(&head, 6); // Insert 7 at the beginning. So linked list becomes 7->6->NULL insert_begin(&head, 7); // Insert 1 at the beginning. So linked list becomes 1->7->6->NULL insert_begin(&head, 1); // Insert 4 at the end. So linked list becomes 1->7->6->4->NULL insert_end(&head, 4);

46 // Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL insertafter(head->right, 8); printf("created DLL is: "); printlist(head); deletenode(&head, head->right->right); /*delete middle node*/ /* Modified linked list will be NULL<-8->NULL */ printf("\n Modified Linked list "); printlist(head); getchar(); return 0;

UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. Simple Queue Implementation with arrays...3 2. Circular queue with global container

More information

Data Structure with C. List

Data Structure with C. List Subject: Data Structure with C Topic: List Introduction list is a finite sequence of data items, i.e. a collection of data items arranged in a certain linear order. he basic operations performed on this

More information

Module III. Linear Data Structures and their Linked Storage Representation

Module III. Linear Data Structures and their Linked Storage Representation Module III Linear Data Structures and their Linked Storage Representation 3.1 Linked List Linked list is the collection of inter connected nodes with a head node representing the first node and a tail

More information

Procedural Programming

Procedural Programming Exercise 6 (SS 2016) 04.07.2015 What will I learn in the 6. exercise Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Home exercise 4 (3 points) Write a program which is able to handle

More information

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming & Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Eight: Math Functions, Linked Lists, and Binary Trees Name: First Name: Tutor:

More information

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data

More information

Solution for Data Structure

Solution for Data Structure Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT

More information

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 Introduction to Linked Lists Each bead connected to the next through a

More information

Data Structures and Applications (17CS33)

Data Structures and Applications (17CS33) 3.7 Reversing a Singly Linked List #include #include struct node int data; struct node *next; ; void insert_list(int); void display(); void revers(); struct node *head,*newnode,*tail,*temp,*temp1,*temp2;

More information

This document can be downloaded from with most recent updates. 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS

This document can be downloaded from   with most recent updates. 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS 4.1 MEMORY MANAGEMENT 4.1.1 Basics about Memory When a C program is compiled, the compiler translates the source code into machine code. Now the

More information

DS Lab Manual -17CS33

DS Lab Manual -17CS33 Chethan Raj C Assistant Professor Dept. of CSE 6. Design, Develop and Implement a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with

More information

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List 1 EEE 212 Algorithms & Data Structures Spring 05/06 Lecture Notes # 13 Outline Doubly Linked Lists Linear & Circular Doubly Linked Lists Primitive Functions in Doubly Linked Lists Application of the Doubly

More information

UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. C Program to Check for balanced parenthesis by using Stacks...3 2. Program

More information

Basically queue is nothing but an array or a vector with a maximum capacity of size. Front=1 Front=1 REAR=2. Front=1 REAR=3 Front=1 REAR=4 Q is Full

Basically queue is nothing but an array or a vector with a maximum capacity of size. Front=1 Front=1 REAR=2. Front=1 REAR=3 Front=1 REAR=4 Q is Full Queue A Queue is defined as linear list in which insertion is taking place at one end and deletion is taking place at the other end. The end where insertion is taking place is called as rear end. The end

More information

UNIT-2 Stack & Queue

UNIT-2 Stack & Queue UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

Procedural Programming & Fundamentals of Programming

Procedural Programming & Fundamentals of Programming & Fundamentals of Programming Exercise 4 (SS 2018) 12.06.2018 What will I learn in the 5. exercise Files Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Files A file can be seen as

More information

Now consider the following situation after deleting three elements from the queue...

Now consider the following situation after deleting three elements from the queue... Scheme of valuvation -1I Subject & Code : Data Structure and Application (15CS33) NOTE: ANSWER All FIVE QUESTIONS 1 Explain the diadvantage of linear queue and how it i olved in circular queue. Explain

More information

Programming. Lists, Stacks, Queues

Programming. Lists, Stacks, Queues Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation

More information

Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use

Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use Module 3 Linear data structures and linked storage representation Advantages of arrays Linear data structures such as stacks and queues can be represented and implemented using sequential allocation ie

More information

Linked Lists and other Dynamic Data Structures

Linked Lists and other Dynamic Data Structures Linked Lists and other Dynamic Data Structures Arrays Fixed in size Allocated in advance within a contiguous memory block Look-up is fast Resizing and Deleting is hard (reallocate and copy) Dynamic Data

More information

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Write a program that creates in the main function two linked lists of characters and fills them with the following values: Write a program that creates in the main function two linked lists of characters and fills them with the following values: The first list will have 3 nodes with the following characters: A,B, and C. The

More information

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS LINKED LISTS

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS LINKED LISTS LINKED LISTS Contents: Linked lists Inserting and removing nodes from a list Linked implementation of stacks getnode and freenode operations Linked implementation of queues Liked list as a data structure

More information

Frequently asked Data Structures Interview Questions

Frequently asked Data Structures Interview Questions Frequently asked Data Structures Interview Questions Queues Data Structure Interview Questions How is queue different from a stack? The difference between stacks and queues is in removing. In a stack we

More information

Stack & Queue on Self-Referencing Structures

Stack & Queue on Self-Referencing Structures C Programming 1 Stack & Queue on Self-Referencing Structures C Programming 2 Representation of Stack struct stack { int data ; struct stack *next ; ; typedef struct stacknode node, *stack ; C Programming

More information

.:: UNIT 3 ::. LIST AND LINKED LIST

.:: UNIT 3 ::. LIST AND LINKED LIST :: UNIT 3 :: LIST AND LINKED LIST 31 A list is a useful structure to hold a collection of data The simplest method to implement a List ADT is to use an array linear list, contiguous list Characteristics

More information

AIM:- To write a C program to create a singly linked list implementation.

AIM:- To write a C program to create a singly linked list implementation. SINGLY LINKED LIST AIM:- To write a C program to create a singly linked list implementation. ALGORITHM:- 1. Start the program. 2. Get the choice from the user. 3. If the choice is to add records, get the

More information

CS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type

More information

Stack & Queue on Self-Referencing Structures

Stack & Queue on Self-Referencing Structures PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 Stack & Queue on Self-Referencing Structures PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 Representation of Stack struct stack { int data ; struct

More information

MODULE V: POINTERS & PREPROCESSORS

MODULE V: POINTERS & PREPROCESSORS MODULE V: POINTERS & PREPROCESSORS INTRODUCTION As you know, every variable is a memory-location and every memory-location has its address defined which can be accessed using ampersand(&) operator, which

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering SOLUTION FOR INTERNAL ASSESSMENT TEST 2 Date : 6/10/2017 Marks:

More information

Class / Sem: I CSE / II Semester Subject Code: CS 6202 Subject: Programming and Data Structures I Prepared by T. Vithya Unit IV - LINEAR DATA STRUCTURES STACKS AND QUEUES Stack ADT Evaluating arithmetic

More information

Downloaded from : Algorithm: Implementation QUESTION 1 :

Downloaded from :   Algorithm: Implementation QUESTION 1 : QUESTION 1 : WRITE AN ALGORITHM THAT ACCEPTS A BINARY TREE AS INPUT AND PR INTS ITS HEIGHT TO STANDARD OUTPUT. Algorithm: 1. If tree is empty then return 0 2. Else (a) Get the max depth of left subtree

More information

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER 2017-2018 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : Tutorial# 7 (SOLUTIONS) DATE : 06-DEC-2017 Answer 1 enum

More information

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2 CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.

More information

Merge Sort and Analysis --- Analyzing Recursive Programs. Why are we dealing with merge sort in this course?

Merge Sort and Analysis --- Analyzing Recursive Programs. Why are we dealing with merge sort in this course? Merge Sort and Analysis --- Analyzing Recursive Programs Debdeep Mukhopadhyay IIT Kharagpur Why are we dealing with merge sort in this course? It is a powerful application of Divide and Conquer technique

More information

Single linked list. Program: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node. int info; struct node *next; void main()

Single linked list. Program: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node. int info; struct node *next; void main() Single linked list Program: #include #include #include struct node int info; struct node *next; ; void main() struct node *s,*start,*prev,*new1,*temp,*temp1,*ptemp; int cho,i,j,x,n,p;

More information

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements APS105 Lists Structures Textbook Chapters 10.1, 10.3, 10.4, 10.6 2 Structures Arrays allow a collection of elements All of the same type How to collect elements of different types? Structures; in C: struct

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure using C Model wer Subject Code: 22317 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given

More information

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store) Lecture 29 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 29: Linked Lists 19-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vlado Keselj Previous

More information

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields: Assignment 6 Q1. Create a database of students using structures, where in each entry of the database will have the following fields: 1. a name, which is a string with at most 128 characters 2. their marks

More information

Module III. Linear Data Structures and their Linked Storage Representation

Module III. Linear Data Structures and their Linked Storage Representation Module III Linear Data Structures and their Linked Storage Representation 3.1 Linked List Linked list is the collection of inter connected nodes with a head node representing the first node and a tail

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA INTERNAL ASSESSMENT TEST 2 (Scheme and Solution) Data Structures Using C (16MCA11) 1) A.

More information

Queue: Queue Representation: Basic Operations:

Queue: Queue Representation: Basic Operations: Queue: Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C DATA STRUCTURES USING C LECTURE NOTES Prepared by Dr. Subasish Mohapatra Department of Computer Science and Application College of Engineering and Technology, Bhubaneswar Biju Patnaik

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) The queue ADT A queue is like a "natural" queue of elements. It is an ordered list in which all insertions occur at one end called

More information

Duration: 110 minutes

Duration: 110 minutes EASTERN MEDITERRANEAN UNIVERSITY Computer Engineering Department CMPE-231 DATA STRUCTURES FINAL EXAMINATION 13 June 2014 Duration: 110 minutes Name, Surname. SOLUTION KEY......... Student ID #..........

More information

SINGLE LINKED LIST. Algorithm for allocating memory for the new node

SINGLE LINKED LIST. Algorithm for allocating memory for the new node SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA SCSVMV UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Faculty: S.Gokulakrishnan AP/CSE SINGLE LINKED LIST Aim:- Write a C program to implement

More information

Sree Vidyanikethan Engineering College Sree Sainath Nagar, A. Rangampet

Sree Vidyanikethan Engineering College Sree Sainath Nagar, A. Rangampet Sree Vidyanikethan Engineering College Sree Sainath Nagar, A. Rangampet 517 102 Department of Computer Science and Engineering I B. Tech C Programming Language Lab Title: Stack SVEC/IT/EXPT-CP-21 PROBLEM

More information

INSERT AS A FIRST NODE

INSERT AS A FIRST NODE CIRCULAR LINKED LIST Insert as a first node Insert as a last node Delete first node Delete last node Insert after a node Insert before a node Search Traverse INSERT AS A FIRST NODE void insertf() struct

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code: Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

Model Solution for QP CODE : ( 3 Hours )

Model Solution for QP CODE : ( 3 Hours ) Model Solution for QP CODE : 24788 ( 3 Hours ) All answers are for reference only. Any alternate answer that solves the problem should be considered eqully valid. 1. (a) Define data structure? Give its

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Introduction In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station etc. The person who come first, he/she

More information

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

STACKS 3.1 INTRODUCTION 3.2 DEFINITION STACKS 3 3.1 INTRODUCTION A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3 Linear and Non-Linear Data Structures Linear data structure: Linear data structures are those data structure in which data items are arranged in a linear sequence by physically or logically or both the

More information

Unit-2. Stacks: Introduction-Definition-Representation of Stack-Operations on Stacks- Applications of Stacks.

Unit-2. Stacks: Introduction-Definition-Representation of Stack-Operations on Stacks- Applications of Stacks. Unit-2 Stacks: Introduction-Definition-Representation of Stack-Operations on Stacks- Applications of Stacks. Queues: Introduction, Definition- Representations of Queues- Various Queue Structures- Applications

More information

DEV BHOOMI INSTITUTE OF TECHNOLOGY

DEV BHOOMI INSTITUTE OF TECHNOLOGY DEV BHOOMI INSTITUTE OF TECHNOLOGY Department of Computer Science and Engineering Year: 2nd Semester: 3rd Data Structures- PCS-303 LAB MANUAL Prepared By: HOD(CSE) DEV BHOOMI

More information

DC54 DATA STRUCTURES JUNE 2013

DC54 DATA STRUCTURES JUNE 2013 Q 2 (a) Define storage class and its functions. Explain in detail scope, storage allocation and purpose of each storage class. 'Storage' refers to the scope of a variable and memory allocated by compiler

More information

CS32 Discussion Week 3

CS32 Discussion Week 3 CS32 Discussion Week 3 Muhao Chen muhaochen@ucla.edu http://yellowstone.cs.ucla.edu/~muhao/ 1 Outline Doubly Linked List Sorted Linked List Reverse a Linked List 2 Doubly Linked List A linked list where

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

More information

(Section : Computer Science)

(Section : Computer Science) (Section : Computer Science) 26. What will happen if we compile and execute the following program? static int count = 20; int main(void) { int i = 0; static int count = 6; while (i < count) { i++; count--;

More information

/****************************************** Application: Linked List Example Compiled on: Borland Turbo C++ 3.0

/****************************************** Application: Linked List Example Compiled on: Borland Turbo C++ 3.0 /****************************************** Application: Linked List Example Compiled on: Borland Turbo C++ 3.0 ******************************************/ #include #include /* Structure

More information

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list. Leaning Objective: In this Module you will be learning the following: Circular Linked Lists and it operations Introduction: Circular linked list is a sequence of elements in which every element has link

More information

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015 Solutions Q.1 a. What is a pointer? Explain how it is declared and initialized. (4) Different from other normal variables which can store values, pointers are special variables that can hold the address

More information

CMPE231 DATA STRUCTURES FINAL EXAMINATION / FALL 2010

CMPE231 DATA STRUCTURES FINAL EXAMINATION / FALL 2010 EASTERN MEDITERRANEAN UNIVERSITY Computer Engineering Department CMPE231 DATA STRUCTURES FINAL EXAMINATION / FALL 2010 Lecturers: Prof.Dr.Marifi Güler (group 1) Prof.Dr.Erden Başar (group 2) January 14,

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 17 EXAMINATION Subject Name: Data Structure Using C Model Answer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as

More information

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from

More information

Vivekananda College of Engineering & Technology. Data Structures and Applications

Vivekananda College of Engineering & Technology. Data Structures and Applications Vivekananda College of Engineering & Technology [Sponsored by Vivekananda Vidyavardhaka Sangha, Puttur ] Affiliated to Visvesvaraya Technological University Approved by AICTE New Delhi & Govt of Karnataka

More information

/* 1. WAP TO PERFORM PUSH,POP,AND DISPLAY OPERATION ON A STACK USING LINEAR ARRAY */

/* 1. WAP TO PERFORM PUSH,POP,AND DISPLAY OPERATION ON A STACK USING LINEAR ARRAY */ /* 1. WAP TO PERFORM PUSH,POP,AND DISPLAY OPERATION ON A STACK USING LINEAR ARRAY */ #include #include int i,top,ch,s[3],item; void push(),pop(),dis(); main() top=-1; ch=0; while(ch!=4)

More information

Application of Stack (Backtracking)

Application of Stack (Backtracking) Application of Stack (Backtracking) Think of a labyrinth or maze How do you find a way from an entrance to an exit? Once you reach a dead end, you must backtrack. But backtrack to where? to the previous

More information

Government Girls Polytechnic, Bilaspur

Government Girls Polytechnic, Bilaspur Government Girls Polytechnic, Bilaspur Name of the Lab: Programming Lab Practical: Data Structure Lab Class: 4 th Semester (Computer Science & Engineering) Teachers Assessment: 30 End Semester Examination:70

More information

Pointers. Array. Solution to the data movement in sequential representation

Pointers. Array. Solution to the data movement in sequential representation 1 LISTS Pointers Array sequential representation some operation can be very time-consuming (data movement) size of data must be predefined static storage allocation and deallocation Solution to the data

More information

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Darshan Institute of Engineering & Technology for Diploma studies Unit 4 Pointer A pointer is a variable that contains address or location of another variable. Pointer is a derived data type in C. Pointers contain memory address as their values, so they can also be used to

More information

Module 04 Trees Contents

Module 04 Trees Contents Module 04 Trees Contents Chethan Raj C Assistant Professor Dept. of CSE 1. Trees Terminology 2. Binary Trees, Properties of Binary trees 3. Array and linked Representation of Binary Trees 4. Binary Tree

More information

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? The heterogeneous linked list contains different data types in its nodes and we need a link

More information

Information Technology Association Department of Information Technology MIT Campus, Anna University Chennai

Information Technology Association Department of Information Technology MIT Campus, Anna University Chennai Algorithm Puzzles - Week1 Solutions 1. Swap two integer variables without using temporary variable. Give at least two different solutions. The first solution is a=a+b; b=a-b; a=a-b; Hope most of you are

More information

The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty. EENG212 Algorithms & Data Structures Fall 0/07 Lecture Notes # Outline Stacks Application of Stacks Reversing a String Palindrome Example Infix, Postfix and Prefix Notations APPLICATION OF STACKS Stacks

More information

POLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition.

POLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition. POLYNOMIAL ADDITION AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition. ALGORITHM:- 1. Start the program 2. Get the coefficients and powers

More information

Computer Systems and Networks

Computer Systems and Networks University of the Pacific LECTURE 5: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) Today s Class o Pointer basics o Pointers and mul;- dimensional arrays

More information

Answer to Problem Set 2 Out: 15 September, 1995

Answer to Problem Set 2 Out: 15 September, 1995 Burt Rosenberg Math 220/317: Programming II/Data Structures 1 Answer to Problem Set 2 Out: 15 September, 1995 /* * Answer to Problem Set 2 * Univ. of Miami, Math 220/317 * Fall 1995 * Prof. B. Rosenberg

More information

Chapter 4: Lists 2004 년가을학기. 강경란 정보및컴퓨터공학부, 아주대학교

Chapter 4: Lists 2004 년가을학기. 강경란 정보및컴퓨터공학부, 아주대학교 Chapter 4: Lists 2004 년가을학기 강경란 korykang@ajou.ac.kr 정보및컴퓨터공학부, 아주대학교 1. Pointers 2. Singly Linked Lists Chapter 4 Lists 3. Dynamically Linked Stacks and Queues 4. Polynomials 5. Additional List Operations

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Stack and Queue Practice Sheet #13 Date: 04-04-2017 1. Consider the following sequence of push and pop operations

More information

MC9217 / Programming and Data Structures Lab MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C

MC9217 / Programming and Data Structures Lab MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C 0 0 3 2 1.Create a Stack and do the following operations using arrays and linked lists (i)push (ii) Pop (iii) Peep 2.Create a Queue and do the following

More information

DATA STRUCTURE. o DYNAMIC MEMORY. ALLOCATION o SELF-REFERENTIAL STRUCTURE o TYPEDEF LINK LIST STACK QUEUE TREE

DATA STRUCTURE. o DYNAMIC MEMORY. ALLOCATION o SELF-REFERENTIAL STRUCTURE o TYPEDEF LINK LIST STACK QUEUE TREE DATA STRUCTURE TOPICS PAGE o DYNAMIC MEMORY 2-5 ALLOCATION o SELF-REFERENTIAL STRUCTURE o TYPEDEF LINK LIST 5-16 STACK 17-28 o APPLICATION OF STACK 28-38 QUEUE 39-43 TREE o BINARY SEARCH TREE o HEAP GRAPH

More information

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists Outline Briefly review the last class Pointers and Structs Memory allocation Linked lists C Structures and Memory Allocation A struct is a data structure that comprises multiple types, each known as a

More information

Guide for The C Programming Language Chapter 5

Guide for The C Programming Language Chapter 5 1. Differentiate between primitive data type and non-primitive data type. Primitive data types are the basic data types. These data types are used to represent single values. For example: Character, Integer,

More information

S.Y. Diploma : Sem. III [CO/CM/IF/CD/CW] Data Structure using C

S.Y. Diploma : Sem. III [CO/CM/IF/CD/CW] Data Structure using C S.Y. Diploma : Sem. III [CO/CM/IF/CD/CW] Data Structure using C Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 100 Q.1 (a) Attempt any SIX of the following : [12] Q.1 (a) (i) Define time complexity

More information

NCS 301 DATA STRUCTURE USING C

NCS 301 DATA STRUCTURE USING C NCS 301 DATA STRUCTURE USING C Unit-1 Part-4 Linked Lists Hammad Mashkoor Lari Assistant Professor Allenhouse Institute of Technology www.ncs301ds.wordpress.com Introduction List refers to linear collection

More information

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 3 Pointers (revisited) int i = 4, j = 6, *p = &i, *q = &j, *r; if (p == &i)...; if (p == (& i))...;... = **&p;... = *(*(& p));... = 9 * *p / *q + 8;... = (((9*(*p)))/(*q)) + 8; *(r = &i)

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

CS 261 Data Structures. AVL Trees

CS 261 Data Structures. AVL Trees CS 261 Data Structures AVL Trees AVL Implementation struct AVLNode { TYPE val; struct AVLNode *left; struct AVLNode *rght; int hght; /* Height of node*/ ; Compute the Balance Factor (BF) int _height(struct

More information

EEE Algorithms & Data Structures Final Exam Instructor: Dr. Hasan Demirel

EEE Algorithms & Data Structures Final Exam Instructor: Dr. Hasan Demirel Name: Date: 04 January 2005 Number: EEE 212 - Algorithms & Data Structures Final Exam Instructor: Dr. Hasan Demirel 1 2 3 4 T Read the Following Instructions Carefully: 1. The duration of the exam is strictly

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 LINKED LISTS LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays

Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays Circular Queue: Implementation and Applications In linear queue, when we delete any element, only front increment by one but position is not used later. So, when we perform more add and delete operations,

More information

/*Addition of two polynomials*/ #include<stdio.h> #include<malloc.h> #include<conio.h> struct link { int coeff; int pow; struct link *next; }; struct

/*Addition of two polynomials*/ #include<stdio.h> #include<malloc.h> #include<conio.h> struct link { int coeff; int pow; struct link *next; }; struct /*Addition of two polynomials*/ #include #include #include struct link int coeff; int pow; struct link *next; ; struct link *poly1=null,*poly2=null,*poly=null; void create(struct

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way. Data Structures

More information

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allows for creation

More information

MTH 307/417/515 Final Exam Solutions

MTH 307/417/515 Final Exam Solutions MTH 307/417/515 Final Exam Solutions 1. Write the output for the following programs. Explain the reasoning behind your answer. (a) #include int main() int n; for(n = 7; n!= 0; n--) printf("n =

More information