0% found this document useful (0 votes)
444 views16 pages

Exercise 1 (DSA)

The document describes the implementation of a singly linked list in C programming language. It includes functions to create, insert, delete and display nodes in the linked list. The main function contains a menu to call these functions and display sample input/output.

Uploaded by

Mani Mathu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
444 views16 pages

Exercise 1 (DSA)

The document describes the implementation of a singly linked list in C programming language. It includes functions to create, insert, delete and display nodes in the linked list. The main function contains a menu to call these functions and display sample input/output.

Uploaded by

Mani Mathu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

IMPLEMENTATION OF SINGLY LINKED LIST

#include <stdio.h> #include <conio.h> #include <alloc.h> void create(); void insert(); void delet(); void display(); struct node { int data; struct node *link; }; struct node *first=NULL,*last=NULL,*next,*prev,*cur; void create() { cur=(struct node*)malloc(sizeof(struct node)); printf("\nENTER THE DATA: "); scanf("%d",&cur->data); cur->link=NULL; first=cur; last=cur; } void insert() { int pos,c=1; cur=(struct node*)malloc(sizeof(struct node)); printf("\nENTER THE DATA: "); scanf("%d",&cur->data);

printf("\nENTER THE POSITION: "); scanf("%d",&pos); if((pos==1) &&(first!=NULL)) { cur->link = first; first=cur; } else { next=first; while(c<pos) { prev=next; next=prev->link; c++; } if(prev==NULL) { printf("\nINVALID POSITION\n"); } else { cur->link=prev->link; prev->link=cur; } } } void delet() {

int pos,c=1; printf("\nENTER THE POSITION : "); scanf("%d",&pos); if(first==NULL) { printf("\nLIST IS EMPTY\n"); } else if(pos==1 && first->link==NULL) { printf("\n DELETED ELEMENT IS %d\n",first->data); free(first); first=NULL; } else if(pos==1 && first->link!=NULL) { cur=first; first=first->link; cur->link=NULL; printf("\n DELETED ELEMENT IS %d\n",cur->data); free(cur); } else { next=first; while(c<pos) { cur=next; next=next->link; c++;

} cur->link=next->link; next->link=NULL; if(next==NULL) { printf("\nINVALID POSITION\n"); } else { printf("\n DELETED ELEMENT IS %d\n",next->data); free(next); } } } void display() { cur=first; while(cur!=NULL) { printf("\n %d",cur->data); cur=cur->link; } } void main() { int ch; clrscr(); printf("\n\nSINGLY LINKED LIST"); do

{ printf("\n\[Link]\[Link]\[Link]\[Link]"); printf("\n\nENTER YOUR CHOICE : "); scanf("%d",&ch); switch(ch) { case 1: create(); display(); break; case 2: insert(); display(); break; case 3: delet(); display(); break; case 4: exit(0); default: printf("Invalid choice..."); } }while(1); }

SAMPLE INPUT AND OUTPUT: SINGLY LINKED LIST [Link] [Link] [Link] [Link] ENTER YOUR CHOICE : 1 ENTER THE DATA: 10 10 [Link] [Link] [Link] [Link] ENTER YOUR CHOICE : 2 ENTER THE DATA: 30 ENTER THE POSITION: 1 30 10 [Link] [Link] [Link] [Link] ENTER YOUR CHOICE : 3 ENTER THE POSITION : 2 LIST IS EMPTY

IMPLEMENTATION OF DOUBLY LINKED LIST


#include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #define NULL 0

struct linkedlist { int item; struct linkedlist *right,*left; };

typedef struct linkedlist node;

void main() { node *start,*end; int choice; int menu(void); node *create(node **lastnode); void display(node *first,node *last); void insert(node **first,node **last); void del(node **first,node **last); clrscr(); printf("\n DOUBLY LINKED LIST"); printf("\n ******************"); do {

printf("\n\nMain menu"); printf("\n\[Link] \[Link] \[Link] \[Link] \[Link]"); choice =menu();

switch(choice) {

case 1: printf("\n Enter the data(-999 to stop):"); start=create(&end); continue;

case 2: insert(&start,&end); printf("\n"); continue;

case 3: del(&start,&end); printf("\n"); continue;

case 4: display(start,end); printf("\n"); continue;

case 5: exit(0);

default: printf("\n\nINVALID CHOICE..."); } }while(1); }

int menu() { int choice; do { printf("\n Enter your choice:"); scanf("%d",&choice); if(choice<1||choice>5) printf("\n Wrong choice"); }while(choice<1||choice>5); printf("\n"); return(choice); }

node *create(node **lastnode) { node *temp,*firstnode; int info; *lastnode=NULL; firstnode=NULL; scanf("%d",&info); while(info!=-999) {

temp=(node *)malloc(sizeof(node)); temp->item=info; temp->right=NULL; if(firstnode==NULL) { temp->left=NULL; firstnode=temp; } else { temp->left=(*lastnode); (*lastnode)->right=temp; } (*lastnode)=temp; scanf("%d",&info); } if(firstnode!=NULL) (*lastnode)=temp; return(firstnode); }

void display(node *first,node *last) { printf("\n Forward traversal\n"); while(first!=NULL) { printf("%d\t",first->item); first=first->right; }

printf("\n Backward traversal\n"); while(last!=NULL) { printf("%d\t",last->item); last=last->left; } return; }

void insert(node **first,node **last) { node *newnode; int newitem; int position; node *temp; int i; printf("\n New data item:"); scanf("%d",&newitem); do { printf("\n Position of insertion:"); scanf("%d",&position); }while(position<=0); if(((*first)==NULL)||(position==1)) { newnode=(node *)malloc(sizeof(node)); newnode->item=newitem; newnode->right=*first; newnode->left=NULL;

if((*first)!=NULL) (*first)->left=newnode; else (*last)=newnode; *first=newnode; } else { i=1; temp=*first; while((i<position-1)&&(temp->right!=NULL)) { i++; temp=temp->right; } newnode=(node *)malloc(sizeof(node)); newnode->item=newitem; newnode->right=temp->right; if(temp->right!=NULL) temp->right->left=newnode; newnode->left=temp; temp->right=newnode; } if(newnode->right==NULL) *last=newnode; }

void del(node **first,node **last) {

node *temp,*prev; int target; printf("\n Enter the data to be deleted:"); scanf("%d",&target); if(*first==NULL) printf("\n List is empty"); else if((*first)->item==target) { if((*first)->right==NULL) *first=*last=NULL; else { *first=(*first)->right; (*first)->left=NULL; } } else { temp=*first; prev=NULL; while((temp->right!=NULL)&&(temp->item!=target)) { prev=temp; temp=temp->right; } if(temp->item!=target) printf("\n Element not found"); else {

if(temp==*last) *last=prev; else temp->right->left=temp->left; prev->right=temp->right; } } }

Sample Input Output: Main menu [Link] [Link] [Link] [Link] [Link] Enter your choice:1 Enter the data(-999 to stop): 5 10 15 -999 Main menu [Link] [Link] [Link] [Link] [Link] Enter your choice:2 New data item:20 Position of insertion: Main menu

[Link] [Link] [Link] [Link] [Link] Enter your choice:4 Forward traversal 5 20 10 15 20 Backward traversal 20 15 10 20 5 Main menu [Link] [Link] [Link] [Link] [Link] Enter your choice:3 Enter the data to be deleted:5 Main menu [Link] [Link] [Link] [Link] [Link] Enter your choice:4 Forward traversal 20 10 15 20 Backward traversal 20 15 10 20

You might also like