Sei sulla pagina 1di 4

/* single linked list programms*/ #include<stdio.h> #include<stdlib.

h> void insertnode(); void display(); void delete(); void addatany(); int reversedisplay(); void count(); void search(); void midpoint(); main() { int c,a; while(c>0) { printf("enter your choice:\n"); printf("1:insertbeg\t 2:display\t 3:delete\t 4:addatany\t 5:reve rsedisplay\t 6:count\t 7:exit\t 8:search\t 9:midpoint\n"); scanf("%d",&c); switch(c) { case 1:insertnode(); break; case 2:display(); break; case 3:delete(); break; case 4:addatany(); break; case 5:reversedisplay(); break; case 6:count(); break; case 7:exit(0); case 8:search(); break; case 9:midpoint(); break; } } } struct llnode { int data ; struct llnode *link; }; struct llnode *head=NULL; void insertnode() { int data; printf("calling the addnode function:\n"); struct llnode *newnode,*temp; newnode=(struct llnode *)malloc(sizeof(struct llnode)); printf("enter the DATA:\n"); scanf("%d",&data); temp=head; newnode->data=data;

if(head==NULL) { head=newnode; temp=newnode; } else while(temp->link!=NULL) { temp=temp->link; } temp->link=newnode; temp=temp->link; newnode->link=NULL; } void display() { printf("\t\t calling display function:\n"); struct llnode *temp; temp=head; printf("data\t address\t\n"); while((temp->link)!=NULL) { printf("%d\t",temp->data); printf("%p\t",temp); temp=temp->link; printf("\n"); } printf("%d\t",temp->data); printf("%p\t\n",temp); temp=temp->link; } void delete() { struct llnode *temp,*p; printf("\t\tcalling delete function:\n"); int i=1,key; printf("enter the position for delete:\n"); scanf("%d",&key); temp=head; if(i==key) { temp=head->link; free(head); head=temp; } else { while(i<key-1) { temp=temp->link; i++; } p=temp->link; temp->link=p->link; free(p); } }

void addatany() { int data; struct llnode *temp,*newnode; newnode=(struct llnode *)malloc(sizeof(struct llnode)); printf("\t\tcalling add at begging position:\n"); int i=1,key; printf("enter the position for adding:\n"); scanf("%d",&key); printf("enter the DATA:\n"); scanf("%d",&data); temp=head; newnode->data=data; if(i==key) { temp=newnode; temp->link=head; head=newnode; } else { while(i<key-1) { temp=temp->link; i++; } newnode->link=temp->link; temp->link=newnode; } } int reversedisplay() { struct llnode *temp=NULL,*next=NULL; while( head != NULL) { temp = next; next= head; head = head->link; next->link = temp; } head = next; printf("data\taddress\n"); while(head!=NULL) { printf(" %d\t%p\t", head->data,head); head = head->link; printf("\n"); } } void count() { int cnt=0; struct llnode *temp; temp=head; while(temp->link!=NULL) { temp=temp->link;

cnt++; } cnt++; printf("count=%d\n",cnt); } void search() { struct llnode *temp; int s,c=0; printf("enter the you want to search:\n"); scanf("%d",&s); temp=head; while(temp->link!=NULL) { if(temp->data==s) { printf("\tU'r entered number %d is in the linked list\n" ,s); printf("\tU'r entered number node is %d\n",c); return; } else { temp=temp->link; c++; } } if(temp->data==s) { printf("\tU'r entered number %d is in linked list\n",s); printf("\tU'r entered number node is %d\n",c); return; } } void midpoint() { struct llnode *temp; float first=1.0,second=1.0; temp=head; while(temp->link!=NULL) { first=first+0.5; second=second+1; temp=temp->link; } printf("middle node=%f\n",first); printf("last=%f\n",second); }

Potrebbero piacerti anche