data structure singly linked list programs vtu exams

24
1| Page Ashoka.R, 2 nd Sem Mca, Maharaja Institute Of Technology Mysore. /* Lab10 */ /* Write a c program using dynamic variables and pointers to construct a stack of integers using singly linked list & to perform the following operations: 1.Push(Insert front) 2.Pop(Delete front) 3.Display The program should print approproate messages for stack overflow and stack empty. */ #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *next; }; typedef struct node NODE; void ins_first(NODE**, int); int del_first(NODE**); void display(NODE*); int main() { NODE *first=NULL; int choice, item; for(;;) { printf("Stack Menu\n"); printf("1.Push OR Insert Front\n"); printf("2.Pop OR Delete Front\n"); printf("3.Display\n"); printf("4.Exit\n"); printf("Enter U R Choice\n"); scanf("%d", &choice); printf("\n\n"); switch(choice) { case 1 : printf("Enter The Item To Insert\n"); scanf("%d", &item); printf("\n\n"); ins_first(&first, item); break; case 2 : item=del_first(&first); if(item!='\0') printf("Deleted Element Is %d\n\n", item); break; case 3 : printf("Contents Of Stack Are\n"); display(first); break;

Upload: ashok-seervi-r

Post on 11-May-2015

942 views

Category:

Education


4 download

DESCRIPTION

Complete Programs on Singly Linked List - VTU Exams

TRANSCRIPT

Page 1: Data structure singly linked list programs VTU Exams

1 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

/* Lab10 *//* Write a c program using dynamic variables and pointers to

construct a stack of integers using singly linked list &to perform the following operations:1.Push(Insert front)2.Pop(Delete front)3.DisplayThe program should print approproate messages for stackoverflow and stack empty. */

#include<stdio.h>#include<stdlib.h>

struct node{

int info;struct node *next;

};typedef struct node NODE;

void ins_first(NODE**, int);int del_first(NODE**);void display(NODE*);

int main(){

NODE *first=NULL;int choice, item;for(;;){

printf("Stack Menu\n");printf("1.Push OR Insert Front\n");printf("2.Pop OR Delete Front\n");printf("3.Display\n");printf("4.Exit\n");printf("Enter U R Choice\n");scanf("%d", &choice);printf("\n\n");switch(choice){

case 1 : printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");ins_first(&first, item);break;

case 2 : item=del_first(&first);if(item!='\0')

printf("Deleted Element Is %d\n\n", item);break;

case 3 : printf("Contents Of Stack Are\n");display(first);break;

Page 2: Data structure singly linked list programs VTU Exams

2 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

default : exit(0);}

}return 1;

}

void ins_first(NODE **first, int item){

NODE *newn;newn=(NODE*)malloc(sizeof(NODE));newn->info=item;newn->next=*first;*first=newn;

}

int del_first(NODE **first){

int item;NODE *temp;if(*first==NULL){

printf("Stack IS Underflow\n\n");return('\0');

}temp=*first;item=temp->info;*first=temp->next;free(temp);return item;

}

void display(NODE *first){

NODE *temp;temp=first;if(first==NULL){

printf("Stack Is Empty\n\n");}else{

while(temp->next!=NULL){

printf("%d\n", temp->info);temp=temp->next;

}printf("%d\n\n", temp->info);

}}

Page 3: Data structure singly linked list programs VTU Exams

3 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

INSERT A NODE AS LAST NODE IN THE LIST

ins_last(&first, item);void ins_last(NODE **first, int item){

NODE *newn, *temp;temp=*first; /* This Is Correct */newn=(NODE*)malloc(sizeof(NODE));newn->info=item;newn->next=NULL; /* Insesrting Node As Last Node. */if(*first==NULL)

*first=newn;/* temp=*first; Error In Code Blocks I Should Write Above Only */

else{

while(temp->next!=NULL){

temp=temp->next;}temp->next=newn;

}}

Page 4: Data structure singly linked list programs VTU Exams

4 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

DELETE THE LAST NODE FROM A LIST OF INTEGERS.

int del_last(NODE **first){

NODE *temp, *prev;int item;temp=*first;prev=NULL;if(*first==NULL){

printf("Empty List, Deletion Not Possible\n");return('\0');

}while(temp->next!=NULL){

prev=temp;temp=temp->next;

}if(prev==NULL){

*first=NULL;item=temp->info;free(temp);return item;

}else{

item=temp->info;prev->next=NULL;free(temp);return item;

}}

Page 5: Data structure singly linked list programs VTU Exams

5 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

TO COUNT NO OF NODES IN A LINKED LIST

int count(NODE *first){

NODE *temp;int total=0;temp=first;if(first==NULL){

return total;}while(temp!=NULL){

total+=1;temp=temp->next;

}return total;

}

***OR***

int count(NODE *first){

NODE *temp;int total=0;temp=first;if(first==NULL){

return total;}while(temp->next!=NULL){

total+=1;temp=temp->next;

}total+=1;return total;

}

Page 6: Data structure singly linked list programs VTU Exams

6 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

TO COUNT THE NUMBER OF NODES WITH VALUE K IN A LIST.

void search_item(NODE *first, int item){

NODE *temp;int pos=0, times=0;temp=first; /*IMP*/if(first==NULL)

printf("LISt Is Empty\n");else{

while(temp!=NULL){

pos+=1;if(temp->info==item){

printf("%d Found At %d Position\n", item, pos);times+=1;

}temp=temp->next;

}printf("%d Found %d Times", item, times);

}}

Page 7: Data structure singly linked list programs VTU Exams

7 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WRITE A PROGRAM TO SEARCH A NODE WITH A VALUE K , IF IT IS NOT FOUND THEN INSERT VALUE ASLAST NODE.

void k_insert(NODE **first, int item){

NODE *temp, *newn;newn=(NODE*)malloc(sizeof(NODE));int flag;flag=0;

newn->info=item;newn->next=NULL;if(*first==NULL)

*first=newn;else{

temp=*first;while(temp!=NULL && flag==0){

if(temp->info==item)flag=1; /* flag=1 When Item Found */

temp=temp->next;}

}

if(flag==1)printf("%d Found\n", item);

else{

temp=*first;while(temp->next!=NULL)

temp=temp->next;temp->next=newn;

}}

Page 8: Data structure singly linked list programs VTU Exams

8 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WAP TO SEARCH A LIST FOR A NODE WITH VALUE K, IF IT IS FOUND THEN DELETE THAT NODE.

case 5 : printf("Enter The Key To Delete\n");scanf("%d", &key);item=del_key(&first, key);if(item!='\0')

printf("%d Is Deleted.\n", item);break;

int del_key(NODE **first, int key){

int item;NODE *temp, *prev;if(*first==NULL){

printf("List IS Empty\n");return('\0');

}temp=*first;if(key==temp->info){

item=temp->info;*first=temp->next;free(temp);return item;

}prev=NULL;while(temp!=NULL){

if(key==temp->info)break;

prev=temp;temp=temp->next;

}if(temp!=NULL){

item=temp->info;prev->next=temp->next;free(temp);return item;

}printf("Key Not Found In The List\n");return('\0');

}

Page 9: Data structure singly linked list programs VTU Exams

9 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WAP TO INSERT A NODE INTO A LINKED LIST AT A GIVEN POSTION.

void ins_pos(NODE** first, int item, int pos){

NODE *newn, *temp, *prev;int k;newn=(NODE*)malloc(sizeof(NODE));newn->info=item;if(*first==NULL || pos==1){

newn->next=*first;*first=newn;

}else{

temp=*first;prev=NULL;k=1;while(temp!=NULL && k<pos){

prev=temp;temp=temp->next;k+=1;

}newn->next=prev->next;prev->next=newn;

}}

Page 10: Data structure singly linked list programs VTU Exams

10 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WAP TO DELETE A NODE AT THE SPECIFIED POSTION.

int del_pos(NODE **first, int pos){

int item, k=0;NODE *temp, *prev;if(*first==NULL || pos<=0){

printf("Invalid Postion\n");return('\0');

}if(pos==1){

temp=*first;item=temp->info;*first=temp->next;free(temp);return(item);

}temp=*first;prev=NULL;k=1;while(temp!=NULL){

if(k==pos) /* If Found Go Out Of The LOOP */break;

prev=temp;temp=temp->next;k+=1;

}if(k!=pos) /* Pos Found So Delete The Node */{

printf("Invalid Postion\n");return('\0');

}else /*If End OF List, Invalid Postion */{

item=temp->info;prev->next=temp->next;free(temp);return(item);

}}

Page 11: Data structure singly linked list programs VTU Exams

11 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WRITE A C FUNCTION TO INSERT A NODE WITH VALUE X TO THE RIGHT OF NODE WITH VALUE Y IN ASINGLE LINKED LIST.

case 7 : printf("What's The Value Of Y\n");scanf("%d", &y);printf("What's The Value Of X\n");scanf("%d", &x);ins_x_right_to_y(&first, x, y);break;

void ins_x_right_to_y(NODE **first, int x, int y){

NODE *newn, *temp, *prev;newn=(NODE*)malloc(sizeof(NODE));newn->info=x;temp=*first;prev=NULL;if(*first==NULL)

printf("List Is Empty, y Not Found\n");if(y==temp->info) /* if key found in 1st postion */{

newn->next=temp->next;temp->next=newn;

}else{

while(temp!=NULL){

if(y==temp->info)break;

prev=temp;temp=temp->next;

}if(y==temp->info){

newn->next=temp->next;temp->next=newn;

}elseprintf("Key Not Found\n");

}}

Page 12: Data structure singly linked list programs VTU Exams

12 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WRITE A C FUNCTION TO INSERT A NODE WITH VALUE X TO THE LEFT OF NODE WITH VALUE Y IN ASINGLE LINKED LIST.

case 8 : printf("What's The Value Of Y\n");scanf("%d", &y);printf("What's The Value Of X\n");scanf("%d", &x);ins_x_left_to_y(&first, x, y);break;

void ins_x_left_to_y(NODE **first, int x, int y){

NODE *newn, *temp, *prev;newn=(NODE*)malloc(sizeof(NODE));newn->info=x;temp=*first;prev=NULL;if(*first==NULL)

printf("List Is Empty, y Not Found\n");if(y==temp->info) /* if key found in 1st postion */{

newn->next=*first;*first=newn;

}else{

while(temp!=NULL){

if(y==temp->info)break;

prev=temp;temp=temp->next;

}if(y==temp->info){

newn->next=prev->next;prev->next=newn;

}else

printf("Key Not Found\n");}

}

Page 13: Data structure singly linked list programs VTU Exams

13 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

TO SEARCH FOR NODE WITH VALUE Y IN A LIST, IF IT IS FOUND THEN DELETE THE NODE(SAY X) NEXTTO IT.

case 9 : printf("Enter Value For Y\n");scanf("%d", &y);item=del_x_right_to_y(&first, y);if(item!=0)printf("%d Is Deleted.\n", item);

break;

int del_x_right_to_y(NODE **first, int y){

NODE *temp, *prev, *after;int x, flag=0;if(*first==NULL){

printf("List IS Empty\n");return 0;

}temp=*first;if(y==temp->info && temp->next==NULL){

printf("Item Found In 1st Position\n");printf("No Further Element Present Right To It. So Deletion Not Possible\n");return 0;

}prev=NULL;after=NULL;while(temp!=NULL){

if(y==temp->info){

after=temp->next;flag=1;break;

}prev=temp;temp=temp->next;

}if(flag==0){

printf("Y Not Found\n");return 0;

}

Page 14: Data structure singly linked list programs VTU Exams

14 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

if(flag==1){

if(temp->next!=NULL){

x=after->info;temp->next=after->next;free(after);return x;

}else{

printf("Item Found In Last Position, No item Present Right To it\n");printf("So Deletion Not Possible\n");return 0;

}}

}

Page 15: Data structure singly linked list programs VTU Exams

15 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

TO SEARCH FOR NODE WITH VALUE Y IN A LIST, IF IT IS FOUND THEN DELETE THE NODE(SAY X)LEFT(BACK) TO IT.

case 10 : printf("Enter Value For Y\n");scanf("%d", &y);item=del_x_left_to_y(&first, y);if(item!=0)

printf("%d Is Deleted.\n", item);break;

int del_x_left_to_y(NODE **first, int y){

NODE *temp, *prev, *p2p;int x, flag=0;if(*first==NULL){

printf("List IS Empty\n");return 0;

}temp=*first;if(y==temp->info){

printf("Item Found In 1st Position\n");printf("No Further Element Present Left To It. So Deletion Not Possible\n");return 0;

}prev=NULL;p2p=NULL;while(temp!=NULL){

if(y==temp->info){

flag=1;break;

}p2p=prev;prev=temp;temp=temp->next;

}if(flag!=1){

printf("Y Not Found In List\n");return 0;

}

Page 16: Data structure singly linked list programs VTU Exams

16 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

else{

if(p2p==NULL){

x=prev->info;*first=prev->next;free(prev);return x;

}else{

x=prev->info;p2p->next=prev->next;free(prev);return x;

}}

}

Page 17: Data structure singly linked list programs VTU Exams

17 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

WAP TO REVERSE A LIST WITHOUT CREATING NEW NODES.

Page 18: Data structure singly linked list programs VTU Exams

18 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

5 SecondSemester M.C.A. Degree Examination, July 2006a. Write an algorithm to perform each of the following operations on a linked list.i) Append an element to the end of a list.(Page No 3)ii) Free all the nodes in a list. (Page No 18)iii) Delete every second element from a list. (Page No 19)iv) Concatenate two lists. (Page No 20 And 23)v) Delete the last element from a list. ( Page No 4) (10 Marks))

ii) Free all the nodes in a list.

case 11 : printf("Deleting All The Nodes In A List...\n");free_all_nodes(&first);break;

void free_all_nodes(NODE **first){

NODE *temp, *arc;int item;if(*first==NULL){

printf("List Is Already Empty\n");}else{

temp=*first;while(temp!=NULL){

item=temp->info;*first=temp->next;arc=temp;temp=temp->next;free(arc);printf("%d Is Deleted\n", item);

}printf("All The Elements In The List Is Sucessfully Deleted\n");

}}

Page 19: Data structure singly linked list programs VTU Exams

19 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

iii) Delete every second element from a list.

void del_every_2nd_element(NODE **first){

NODE *temp, *prev, *arc;int item, k;if(*first==NULL)

printf("List Is Empty\n");else{

temp=*first;prev=NULL;k=0;while(temp!=NULL){

k+=1;if(k%2==0){

item=temp->info;prev->next=temp->next;arc=temp;free(arc);printf("%d Is Deleted\n", item);

}prev=temp;temp=temp->next;

}if(k==1)

printf("Only 1 Element Present In List So Deletion Not Possible\n");else

printf("All 2nd Elements In The List Is Sucessfully Deleted\n");}

}

Page 20: Data structure singly linked list programs VTU Exams

20 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

iv) Concatenate two lists.

void concat(NODE **first1, NODE **first2){

NODE *temp;if(*first1==NULL)

*first1=*first2;else{

temp=*first1;while(temp->next!=NULL)

temp=temp->next;temp->next=*first2;

}}

Also See Page No 23 For Better Understanding..

Page 21: Data structure singly linked list programs VTU Exams

21 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

Assume A Linked List Of Integers In Assending Order And Develop The Program To Insert A Node WithValue Into A Appropriate Postion.

case 13 : printf("Enter The Item To Insert\n");scanf("%d", &item);ins_ele_assend_list(&first, item);

break;

void ins_ele_assend_list(NODE **first,int item){

NODE *newn, *temp, *prev;newn=(NODE*)malloc(sizeof(NODE));newn->info=item;if(*first==NULL){

newn->next=NULL;*first=newn;

}else{

temp=*first;prev=NULL;while(temp!=NULL && item>=temp->info)/* OR while(temp!=NULL && temp->info<=item) */{

prev=temp;temp=temp->next;

}if(prev==NULL){

newn->next=*first;*first=newn;

}else{

newn->next=prev->next;prev->next=newn;

}}

}

Page 22: Data structure singly linked list programs VTU Exams

22 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

Write A C Routine To Delete All The Nodes Whose Information Field Contains The Value X From ASingly Linked List.

void del_all_x(NODE **first, int x){

NODE *temp, *prev;int count=0, item, k=0;if(*first==NULL)

printf("List Is Empty\n");else{

temp=*first;prev=NULL;

while(temp!=NULL){

if(temp->info==x && prev==NULL)/* If We Miss prev==NULL From if Condition Mean Time If We Give The Input As 12 13 12 All TheseElements Are deleted */

{item=temp->info;*first=temp->next;count+=1;

}else if(temp->info==x){

item=temp->info;prev->next=temp->next;free(temp);count+=1;

}else

prev=temp;temp=temp->next;

}if(count>0)

printf("x=%d Found %d Times & It's Deleted\n", x, count);else

printf("x=%d Not Found\n", x);}

}

Page 23: Data structure singly linked list programs VTU Exams

23 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

/* Concatenate two lists. */

#include<stdio.h>#include<stdlib.h>

struct node{

int info;struct node *next;

};typedef struct node NODE;

void ins_first(NODE**, int);int del_first(NODE**);void display(NODE*);void concat(NODE**, NODE**);

int main(){

NODE *first1=NULL;NODE *first2=NULL;int choice, item;for(;;){

printf("Stack Menu\n");printf("1.In First List To Insert Front\n");printf("2.In Second List To Insert Front\n");printf("3.To Display First List\n");printf("4.To Display Second List\n");printf("5.To Concate Both List & To Display\n");printf("6.Exit\n");printf("Enter U R Choice\n");scanf("%d", &choice);printf("\n\n");switch(choice){

case 1 : printf("First List\n");printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");ins_first(&first1, item);break;

case 2 : printf("Second List\n");printf("Enter The Item To Insert\n");scanf("%d", &item);printf("\n\n");ins_first(&first2, item);break;

case 3 : printf("Contents Of First List\n");display(first1);break;

case 4 : printf("Contents Of Second List\n");display(first2);break;

Page 24: Data structure singly linked list programs VTU Exams

24 | P a g eAshoka.R, 2nd Sem Mca, Maharaja Institute Of Technology Mysore.

case 5 : printf("Concating List Wait....\n");concat(&first1, &first2);printf("After Concating Elements In List Is\n");display(first1);break;

default : exit(0);}

}return 1;

}

void ins_first(NODE **first, int item){

NODE *newn;newn=(NODE*)malloc(sizeof(NODE));newn->info=item;newn->next=*first;*first=newn;

}

void display(NODE *first){

NODE *temp;temp=first;if(first==NULL){

printf("List Is Empty\n\n");}else{

while(temp->next!=NULL){

printf("%d\n", temp->info);temp=temp->next;

}printf("%d\n\n", temp->info);

}}

void concat(NODE **first1, NODE **first2){

NODE *temp;if(*first1==NULL)

*first1=*first2;else{

temp=*first1;while(temp->next!=NULL)

temp=temp->next;temp->next=*first2;

}}