Sei sulla pagina 1di 13

#include<stdio.

h> #define SPACE_SIZE 6 typedefstruct { int roll; char name[20]; unsignedint link; }list; listcursorlist[SPACE_SIZE]; voidinitcursorlist(); intisempty(int head); intgetnode(); voidreadnode(intnewnode); voidreleasenode(intnewnode); voidcreatelist(int *headptr); voidinsertfirst(int *headptr); voidinsertlast(int *headptr); voidinsertmiddle(int *headptr); voiddeletefirst(int *headptr); voiddeletelast(int *headptr); voiddeletemiddle(int *headptr); voidmodifynode(int head); voidviewlist(int head); voidcountlist(int head); intsearchnode(int head); voiddisplaymenu(); void main() { int head=NULL,ch,status,count; clrscr(); initcursorlist(); displaymenu(); while(1) { fflush(stdin); printf("\n\n?"); scanf("%d",&ch); switch(ch) {

case 0: createlist(&head); break; case 1: insertfirst(&head); break; case 2: insertlast(&head); break; case 3: insertmiddle(&head); break; case 4: deletefirst(&head); break; case 5: deletelast(&head); break; case 6: deletemiddle(&head); break; case 7: modifynode(head); break; case 8: viewlist(head); break; case 9: count=count_list(head); printf("no.of nodes in list is %d",count); break; case 10: displaymenu(); break; default: printf("end of run of program..."); exit(0); } } }

voiddisplaymenu() { printf("\n\n basic operations are...."); printf("\n\t0.create list"); printf("\n\t1.insert first"); printf("\n\t2.insert last"); printf("\n\t3.insert middle"); printf("\n\t4.delete first"); printf("\n\t 5.delete last\n\t6.delete middle\n\t 7.modify node\n\t 8.view list\n\t 9.count list\n\t 10.search node\n\t 11.display menu\n\t 12.exit"); } voidinitcursorlist() { int i; for(i=0;i<SPACE_SIZE;i++) cursorlist[i].link=i+1; cursorlist[SPACE_SIZE-1].link=NULL; } intgetnode() { int p; p=cursorlist[0].link; if(p==NULL) return -1; cursorlist[0].link=cursorlist[p].link; cursorlist[p].link=NULL; return p; } voidreadnode(intnewnode) { printf("\n enter roll no"); scanf("%d",&cursorlist[newnode].roll); printf("\n enter name:"); scanf("%s",cursorlist[newnode].name); cursorlist[newnode].link=NULL; }

voidreleasenode(intnewnode) { cursorlist[newnode].link=cursorlist[0].link; cursorlist[0].link=newnode; } intisempty(int head) { if(head==NULL) return 1; else return 0; } voidcreatelist(int *headptr) { int head=NULL,newnode,last; charch; do { newnode=getnode(); if(newnode==-1) { printf("\n memory is not available"); return; } readnode(newnode); if(isempty(head)) { head=newnode; last=head; } else { cursorlist[last].link=newnode; last=cursorlist[last].link; } fflush(stdin); printf("do u wish to add data in list(y/n)?"); scanf("%c",&ch);

} while((ch=='y')||(ch=='y')); *headptr=head; } voidinsertfirst(int *headptr) { inthead,newnode; head=*headptr; newnode=getnode(); if(newnode==-1) { printf("\n memory is not available"); return; } readnode(newnode); if(isempty(head)) head=newnode; else { cursorlist[newnode].link=head; head=newnode; } *headptr=head; } voidinsertlast(int *headptr) { inthead,last,newnode; head=*headptr; newnode=getnode(); if(newnode==-1) { printf("\n memory is not available"); return; } readnode(newnode); if(isempty(head)) head=newnode;

else { last=head; while(cursorlist[last].link!=NULL) last=cursorlist[last].link; cursorlist[last].link=newnode; } *headptr=head; } voidinsertmiddle(int *headptr) { inthead,last,newnode,insdata; head=*headptr; if(isempty(head)) { printf("\n cursor list is empty. so new node is head node") ; newnode=getnode(); if(newnode==-1) { printf("\n mem. not available"); return; } readnode(newnode); head=newnode; } else { printf("\n enter roll no after which insertion to be made:"); scanf("%d",&insdata); last=head; while(last!=NULL) { if(cursorlist[last].roll==insdata) { newnode=getnode(); if(newnode==-1) { printf("\n mem. not available");

return; } readnode(newnode); cursorlist[newnode].link=cursorlist[last].link; cursorlist[last].link=newnode; return; } else last=cursorlist[last].link; } printf("insert node is not found"); } *headptr=head; } voiddeletefirst(int *headptr) { inthead,delnode; head=*headptr; if(isempty(head)) { printf("\n cursor list is empty"); return; } delnode=head; head=cursorlist[head].link; printf("\n deleted data is...\n"); printf("\n roll no:%d",cursorlist[delnode].roll); printf("\n name:%s",cursorlist[delnode].name); releasenode(delnode); *headptr=head; } voiddeletelast(int *headptr) { intdelnode,last,prev,head=*headptr; if(isempty(head)) { printf("\n cursor list is empty"); return;

} else if(cursorlist[head].link==NULL) { delnode=head; head=NULL; } else { last=head; while(cursorlist[last].link!=NULL) { prev=last; last=cursorlist[last].link; } delnode=last; cursorlist[prev].link=NULL; } printf("\n deleted data is...\n"); printf("\n roll no:%d",cursorlist[delnode].roll); printf("\n name:%s",cursorlist[delnode].name); releasenode(delnode); *headptr=head; } voiddeletemiddle(int *headptr) { inthead,delnode,last,prev,deldata; head=*headptr; if(isempty(head)) { printf("\n cursor list is empty"); return; } printf("\n enter the node roll no to be deleted"); scanf("%d",&deldata); if(cursorlist[head].roll==deldata) { delnode=head; head=cursorlist[head].link; printf("\n deleted data is...");

printf("\n roll no:%d",cursorlist[delnode].roll); printf("\n name:%s",cursorlist[delnode].name); releasenode(delnode); *headptr=head; return; } last=cursorlist[head].link; prev=head; while(last!=NULL) { if(cursorlist[last].roll==deldata) { delnode=last; cursorlist[prev].link=cursorlist[last].link; printf("\n deleted data is..."); printf("\n roll no:%d",cursorlist[delnode].roll); printf("\n name:%s",cursorlist[delnode].name); releasenode(delnode); return; } else { last=cursorlist[last].link; prev=cursorlist[prev].link; } } printf("delete node is not found"); *headptr=head; } voidmodifynode(int head) { intmoddata; if(isempty(head)) { printf("\n cursor list is empty") ; return; } printf("\n enter the node roll no for modification");

scanf("%d",&moddata); while(head!=NULL) { if(cursorlist[head].roll==moddata) { printf("\n modify the data of node...\n"); printf("\n enter new roll no:"); scanf("%d",&cursorlist[head].roll); printf("\n enter new name:"); scanf("%s",&cursorlist[head].name); return; } else head=cursorlist[head].link; } printf("modify node is not found"); } voidviewlist(int head) { if(isempty(head)) printf("cursorlist is empty"); for(;head!=NULL;head=cursorlist[head].link) printf("\n roll no:%d\tname:%s",cursorlist[head].roll,cursorlist[head].name); } intcount_list(int head) { int count=0; if(isempty(head)) { printf("cursorlist is empty"); return count; } for(;head!=NULL;head=cursorlist[head].link) count++; return count; }

OUTPUT : basic operations are.... 0.create list 1.insert first 2.insert last 3.insert middle 4.delete first 5.delete last 6.delete middle 7.modify node 8.view list 9.count list 10.search node 11.display menu 12.exit ?0 enter roll no1 entername:chandru do u wish to add data in list(y/n)?y enter roll no2 entername:izz do u wish to add data in list(y/n)?n ?1 enter roll no5 entername:ash ?2 enter roll no3 entername:raj ?3 enter roll no after which insertion to be made:2 enter roll no4 entername:mj

?8 roll no:5 roll no:1 roll no:2 roll no:4 roll no:3

name:ash name:chandru name:izz name:mj name:raj

?4 deleted data is... roll no:5 name:ash ?5 deleted data is... roll no:3 name:raj ?6 enter the node roll no to be deleted4 deleted data is... roll no:4 name:mj ?7 enter the node roll no for modification2 modify the data of node... enter new roll no:5 enter new name:prince ?8 roll no:1 roll no:5

name:chandru name:prince

?9 no.of nodes in list is 2 ?12

Potrebbero piacerti anche