Untitled
unknown
csharp
a month ago
8.1 kB
1
Indexable
Never
Program to insert a node in single linked list #include<stdio.h> #include<stdlib.h> void insertBegin(int); void insertEnd(int); void insertAt(int,int); void display(); int loc; struct node{ int data; struct node *next; }*head=NULL; void main() { int val,ch; while(1) { printf("\nenter your choice 1.begin 2. end 3.location 4.exit\n"); scanf("%d",&ch); switch(ch) { case 1: printf("enter element to be inserted\n"); scanf("%d",&val); insertBegin(val); display(); break; case 2: printf("enter element to be inserted\n"); scanf("%d",&val); insertEnd(val); display(); break; case 3: if(head==NULL)printf("\nList is empty\n"); else{ printf("enter element and location \n"); scanf("%d%d",&val,&loc); insertAt(val,loc); display(); } break; case 4: exit(0); default: printf("wrong choice"); break; } } } void insertBegin(int val) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=val; if(head==NULL) { newnode->next=NULL; head=newnode; } else { newnode->next=head; head=newnode; } } void insertEnd(int val) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=val; newnode->next=NULL; if(head==NULL) { head=newnode; } else { struct node *temp=head; while(temp->next!=NULL) temp=temp->next; temp->next=newnode; } } void insertAt(int val,int loc) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); struct node *temp=head; newnode->data=val; while(temp->data!=loc) temp=temp->next; newnode->next=temp->next; temp->next=newnode; } void display() { struct node *temp=head; if(head==NULL) { printf("List is empty"); } else { while(temp->next!=NULL) { printf("%d--->",temp->data); temp=temp->next; } printf("%d-->NULL",temp->data); } } Program to delete a node in single linked list #include<stdio.h> #include<stdlib.h> void insert(); void deleteBegin(); void deleteEnd(); void deleteAt(int); void display(); int loc; struct node{ int data; struct node *next; }*head=NULL; void main() { int val,ch; insert(); display(); while(1) { end:printf("\nenter your choice to delete at 1.begin 2. end 3.location 4.exit\n"); scanf("%d",&ch); switch(ch) { case 1: deleteBegin(); display(); break; case 2: deleteEnd(); display(); break; case 3: if(head==NULL)printf("\nList is empty\n"); else{ printf("enter value to delete \n"); scanf("%d",&val); deleteAt(val); display(); } break; case 4: exit(0); default: printf("wrong choice"); break; } } } void deleteBegin() { struct node *temp=head; if(head==NULL) { printf("List is empty\n"); } else { head=head->next; free(temp); } } void deleteEnd() { if(head==NULL) { printf("list is empty\n"); } else { struct node *temp1=head,*temp2; if(head->next==NULL) head=NULL; else { while(temp1->next!=NULL) { temp2=temp1; temp1=temp1->next; } temp2->next=NULL; } free(temp1); } } void deleteAt(int val) { struct node *temp1=head,*temp2; while((temp1->data!=val)&&(temp1->next!=NULL)) { temp2=temp1; temp1=temp1->next; } if(temp1->data==val) { temp2->next=temp1->next; free(temp1); } else printf("given node is not present in the list\n"); } void display() { struct node *temp=head; if(head==NULL) { printf("List is empty"); } else { while(temp->next!=NULL) { printf("%d--->",temp->data); temp=temp->next; } printf("%d-->NULL",temp->data); } } void insert() { int i,val; for(i=1;i<=5;i++) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); struct node *temp=head; printf("enter element to be inserted\n"); scanf("%d",&val); newnode->data=val; if(head==NULL) { newnode->next=NULL; head=newnode; } else { newnode->next=head; head=newnode; } } } Program to search for a node in single linked list #include<stdio.h> #include<stdlib.h> void insert(); void search(); void display(); int key; struct node{ int data; struct node *next; }*head=NULL; void main() { int val,ch; insert(); display(); search(); } void search() { struct node *temp=head; printf("\nenter the key to be searched\n"); scanf("%d",&key); while((temp->data!=key)&&(temp->next!=NULL)) temp=temp->next; if(temp->data==key) printf("key is found %d",temp->data); else printf("Key is not found %d",key); } void display() { struct node *temp=head; if(head==NULL) { printf("List is empty"); } else { while(temp->next!=NULL) { printf("%d--->",temp->data); temp=temp->next; } printf("%d-->NULL",temp->data); } } void insert() { int i,val; for(i=1;i<=5;i++) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); struct node *temp=head; printf("enter element to be inserted\n"); scanf("%d",&val); newnode->data=val; if(head==NULL) { newnode->next=NULL; head=newnode; } else { newnode->next=head; head=newnode; } } } Implementation of Stack using Array #include<stdio.h> #include<conio.h> void push(int); void pop(); void display(); int stack[SIZE], top = -1; void main() { int value, choice; clrscr(); while(1){ printf("\n\n***** MENU *****\n"); printf("1. Push\n2. Pop\n3. Display\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the value to be insert: "); scanf("%d",&value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("\nWrong selection!!! Try again!!!"); } } } void push(int value){ if(top == SIZE-1) printf("\nStack is Full!!! Insertion is not possible!!!"); else{ top++; stack[top] = value; printf("\nInsertion success!!!"); } } void pop(){ if(top == -1) printf("\nStack is Empty!!! Deletion is not possible!!!"); else{ printf("\nDeleted : %d", stack[top]); top--; } } void display(){ if(top == -1) printf("\nStack is Empty!!!"); else{ int i; printf("\nStack elements are:\n"); for(i=top; i>=0; i--) printf("%d\n",stack[i]); } } Implementation of Stack using Linked List | C Programming #include<stdio.h> #include<conio.h> struct Node { int data; struct Node *next; }*top = NULL; void push(int); void pop(); void display(); void main() { int choice, value; clrscr(); printf("\n:: Stack using Linked List ::\n"); while(1){ printf("\n****** MENU ******\n"); printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the value to be insert: "); scanf("%d", &value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("\nWrong selection!!! Please try again!!!\n"); } } } void push(int value) { struct Node *newNode; newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; if(top == NULL) newNode->next = NULL; else newNode->next = top; top = newNode; printf("\nInsertion is Success!!!\n"); } void pop() { if(top == NULL) printf("\nStack is Empty!!!\n"); else{ struct Node *temp = top; printf("\nDeleted element: %d", temp->data); top = temp->next; free(temp); } } void display() { if(top == NULL) printf("\nStack is Empty!!!\n"); else{ struct Node *temp = top; while(temp->next != NULL){ printf("%d--->",temp->data); temp = temp -> next; } printf("%d--->NULL",temp->data); } }
Leave a Comment