Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int data;
- struct node* next;
- };
- struct node* head = NULL;
- void menu(){
- printf("\n******************");
- printf("\nEnter your choice:\n");
- printf("1. Insert at Beginning\t");
- printf("\t2. Insert at End\t");
- printf("3. Insert at Position\n");
- printf("4. Delete from Beginning\t");
- printf("5. Delete from End\t");
- printf("6. Delete from Position\n");
- printf("7. Display\n");
- printf("0. Exit");
- printf("\n******************\n");
- }
- void display() {
- if (head == NULL){
- printf("Empty List.\n");
- return;
- }
- struct node* current=head;
- while(current!=NULL){
- printf("%d\t",current->data);
- current=current->next;
- }
- }
- void insertAtFront(){
- struct node* new_node;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("Enter Data in new node:");
- scanf("%d",&new_node->data);
- new_node->next=NULL;
- if (head==NULL) {
- head = new_node;
- return;
- }
- new_node->next=head;
- head = new_node;
- }
- void insertAtEnd(){
- struct node* new_node;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("Enter Data in new node:");
- scanf("%d",&new_node->data);
- new_node->next=NULL;
- if (head==NULL) {
- head = new_node;
- return;
- }
- struct node* current= head;
- while (current->next!=NULL){
- current=current->next;
- }
- current->next=new_node;
- }
- void insertAtPos(){
- struct node* new_node;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("Enter Data in new node:");
- scanf("%d",&new_node->data);
- new_node->next=NULL;
- if (head==NULL){
- printf("Empty list\n");
- }
- int key;
- printf("Enter the key to be searched:");
- scanf("%d",&key);
- if (head->data==key) {
- head->next = new_node;
- return;
- } else {
- struct node* temp = head;
- while (temp!=NULL && temp->data!=key){
- temp=temp->next;
- }
- if(temp==NULL) {
- printf("Key not found in list!\n");
- return;
- }
- new_node->next=temp->next;
- temp->next=new_node;
- printf("Item inserted after %d\n",key);
- }
- }
- void deleteAtFront(){
- if (head==NULL){
- printf("Empty List!\n");
- return;
- }
- struct node* temp=head;
- head=head->next;
- free(temp);
- }
- void deleteAtEnd(){
- if (head==NULL){
- printf("Empty List!\n");
- return;
- }
- struct node *temp = head;
- struct node *prev = head;
- if (head->next==NULL)
- head=NULL;
- else {
- while (temp->next != NULL) {
- prev = temp;
- temp = temp->next;
- }
- prev->next = NULL;
- }
- free(temp);
- }
- void deleteAtPos(){
- int flag=0;
- if (head==NULL){
- printf("Empty List!\n");
- return;
- }
- int key;
- printf("Enter data to be deleted:");
- scanf("%d",&key);
- struct node* temp=head;
- struct node* prev;
- if(head->data==key && head->next==NULL){
- flag=1;
- head=NULL;
- } else if (head->data==key && head->next!=NULL){
- flag=1;
- head=head->next;
- } else{
- while (temp->next!=NULL) {
- prev = temp;
- temp = temp->next;
- if (temp->data==key) {
- flag = 1;
- break;
- }
- }
- if(flag==1){
- prev->next=temp->next;
- }
- }
- if (flag==0) {
- printf("Key not found!");
- return;
- }
- free(temp);
- }
- int main() {
- int arg;
- while(1){
- menu();
- scanf("%d",&arg);
- switch(arg){
- case 1:
- insertAtFront();
- break;
- case 2:
- insertAtEnd();
- break;
- case 3:
- insertAtPos();
- break;
- case 4:
- deleteAtFront();
- break;
- case 5:
- deleteAtEnd();
- break;
- case 6:
- deleteAtPos();
- break;
- case 7:
- display();
- break;
- case 0:
- exit(0);
- default:
- printf("Invalid Argument!");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement