#include<stdio.h>
#include<conio.h>
typedef
struct node
{
int dataPtr;
struct node *link;
}NODE;
typedef
struct
{
int count;
NODE *pos;
NODE *head;
NODE *rear;
int (*compare)(int argu1,int argu2);
}LIST;
int
cmp(int argu1,int argu2)
{
if(argu1==argu2)
return(0);
if(argu1>argu2)
return(+1);
else
return(-1);
}
LIST
*createList(int (*compare) (int argu1,int argu2))
{
/*Local
Declaration*/
LIST *list;
if(list)
{
list->head=NULL;
list->pos=NULL;
list->rear=NULL;
list->count=0;
list->compare=compare;
}
return list;
}
int
_search(LIST *pList,NODE **pPre,NODE **pLoc,int pArgu)
{
#define COMPARE \ (((*pList->compare)
(pArgu,(*pLoc)->dataPtr)))
#define COMPARE_LAST \ ((*pList->compare)
(pArgu,pList->rear->dataPtr))
int result,cnt=0;
*pPre=NULL;
*pLoc=pList->head;
if(pList->count==0)
return 0;
if(COMPARE_LAST>0)
{
*pPre=pList->rear;
*pLoc=NULL;
return 0;
}
while((result=COMPARE)>0)
{
*pPre=*pLoc;
*pLoc=(*pLoc)->link;
cnt++;
}
if(result==0)
return ++cnt;
else
return 0;
}
static
int _insert(LIST *pList,NODE *pPre,int dataInPtr)
{
NODE *pNew;
if(!(pNew=(NODE *)malloc(sizeof(NODE))))
return 0;
pNew->dataPtr=dataInPtr;
pNew->link=NULL;
if(pPre==NULL)
{
pNew->link=pList->head;
pList->head=pNew;
if(pList->count==0)
pList->rear=pNew;
}
else
{
pNew->link=pPre->link;
pPre->link=pNew;
if(pNew->link==NULL)
pList->rear=pNew;
}
(pList->count)++;
return 1;
}
int
addNode(LIST *pList,int dataInPtr)
{
int found,success,i,n;
NODE *p,*pPre,*pLoc;
found=_search(pList,&pPre,&pLoc,dataInPtr);
if(found)
return(+1);
success=_insert(pList,pPre,dataInPtr);
/*display element*/
p=pList->head;
n=pList->count;
for(i=0;i<n;i++)
{
printf("%d\t",p->dataPtr);
p=p->link;
}
if(!success)
return(-1);
return(0);
}
void
_delete(LIST *pList,NODE *pPre,NODE *pLoc)
{
int dataOutPtr;
dataOutPtr=pLoc->dataPtr;
if(pPre==NULL)
pList->head=pLoc->link;
else
pPre->link=pLoc->link;
if(pLoc->link==NULL)
pList->rear=pPre;
(pList->count)--;
free(pLoc);
return;
}
int
removeNode(LIST *pList,int keyPtr)
{
int found,n,i;
NODE *pPre;
NODE *pLoc;
NODE *p;
found=_search(pList,&pPre,&pLoc,keyPtr);
if(found)
{
_delete(pList,pPre,pLoc);
p=pList->head;
n=pList->count;
for(i=0;i<n;i++)
{
printf("%d\t",p->dataPtr);
p=p->link;
}
}
printf("\n");
return found;
}
int
searchList(LIST *pList,int pArgu)
{
int found,pDataOut;
NODE *pPre;
NODE *pLoc;
found=_search(pList,&pPre,&pLoc,pArgu);
if(found)
pDataOut=pLoc->dataPtr;
else
pDataOut=NULL;
return found;
}
int
traverse(LIST *pList,int fromwhere)
{
int dataOut;
int success;
if(fromwhere==0)
{
if(pList->count==0)
success=0;
else
{
pList->pos=pList->head;
dataOut=pList->pos->dataPtr;
printf("%d\t",dataOut);
success=1;
}
}
else
{
if(pList->pos->link==NULL)
success=0;
else
{
pList->pos=pList->pos->link;
dataOut=pList->pos->dataPtr;
printf("%d\t",dataOut);
success=1;
}
}
return success;
}
LIST
* destroyList(LIST *pList)
{
NODE *deletePtr;
if(pList)
{
while(pList->count>0)
{
free(pList->head->dataPtr);
deletePtr=pList->head;
pList->head=pList->head->link;
pList->count--;
free(deletePtr);
}
free(pList);
}
return NULL;
}
void
main()
{
int ch=1;
int item,deleteKey,dataOutPtr;
LIST *pList,*cList;
int opt,n,i,cnt,found,dataOut;
NODE *p,*pre,*loc;
clrscr();
printf("Welcome to exploring linked
lists.\n");
printf("........MENU.......\n");
printf(" 1: CREATELIST \n");
printf(" 2: ADD NODE.\n");
printf(" 3: REMOVE NODE \n");
printf(" 4: LIST COUNT \n");
printf(" 5: SEARCH LIST \n");
printf(" 6: TRAVERSE/DISPLAY \n");
printf(" 7: REVERSE LIST \n");
printf(" 8: DESTROY LIST \n");
printf(" 9: Quit\n");
while(ch)
{
printf("\nEnter the choice
:-> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
pList=createList(cmp);
printf("pList
created\n");
break;
case 2:
printf("Enter the data
: ");
scanf("%d",&item);
addNode(pList,item);
printf("\n");
break;
case 3:
printf("Enter the data
: ");
scanf("%d",&deleteKey);
removeNode(pList,deleteKey);
break;
case 4:
p=pList->head;
cnt=0;
while(p!=NULL)
{
cnt++;
p=p->link;
}
printf("No. of
elements in a list is : %d\n",cnt);
break;
case 5:
printf("Enter the item
: ");
scanf("%d",&item);
found=searchList(pList,item);
if(found)
printf("Element
found at position %d\n",found);
else
printf("Element
not found\n");
break;
case 6:
n=pList->count;
for(i=0;i<n;i++)
{
traverse(pList,i);
}
printf("\n");
break;
case 7:
p=pList->rear;
while(p!=NULL)
{
found=_search(pList,&pre,&loc,p->dataPtr);
printf("%d\t",loc->dataPtr);
p=pre;
}
printf("\n");
break;
case 8:
destroyList(pList);
printf("pList is
destroyed\n");
break;
case 9:
exit(0);
break;
default:
printf("Entered wrong
choice!\n");
break;
}
}
}
OUTPUT:
Welcome
to exploring linked lists.
........MENU.......
1: CREATELIST
2: ADD NODE.
3: REMOVE NODE
4: LIST COUNT
5: SEARCH LIST
6: TRAVERSE/DISPLAY
7: REVERSE LIST
8: DESTROY LIST
9: QUIT.
Enter
the choice :-> 1
pList
created
Enter
the choice :-> 2
Enter
the data : 23
23
Enter
the choice :-> 2
Enter
the data : 12
12 23
Enter
the choice :-> 2
Enter
the data : 56
12 23
56
Enter
the choice :-> 2
Enter
the data : 45
12 23
45 56
Enter
the choice :-> 2
Enter
the data : 85
12 23
45 56 85
Enter
the choice :-> 2
Enter
the data : 41
12 23
41 45 56
85
Enter
the choice :-> 3
Enter
the data : 12
23 41
45 56 85
Enter
the choice :-> 3
Enter
the data : 41
23 45
56 85
Enter
the choice :-> 4
No.
of elements in a list is : 4
Enter
the choice :-> 5
Enter
the item : 56
Element
found at position 3
Enter
the choice :-> 5
Enter
the item : 12
Element
not found
Enter
the choice :-> 21
Entered
wrong choice!
Enter
the choice :-> 6
23 45
56 85
Enter
the choice :-> 7
85 56
45 23
Enter
the choice :-> 8
pList
is destroyed
Enter
the choice :-> 10
#include<conio.h>
typedef
struct node
{
int dataPtr;
struct node *link;
}NODE;
typedef
struct
{
int count;
NODE *pos;
NODE *head;
NODE *rear;
int (*compare)(int argu1,int argu2);
}LIST;
int
cmp(int argu1,int argu2)
{
if(argu1==argu2)
return(0);
if(argu1>argu2)
return(+1);
else
return(-1);
}
LIST
*createList(int (*compare) (int argu1,int argu2))
{
/*Local
Declaration*/
LIST *list;
if(list)
{
list->head=NULL;
list->pos=NULL;
list->rear=NULL;
list->count=0;
list->compare=compare;
}
return list;
}
int
_search(LIST *pList,NODE **pPre,NODE **pLoc,int pArgu)
{
#define COMPARE \ (((*pList->compare)
(pArgu,(*pLoc)->dataPtr)))
#define COMPARE_LAST \ ((*pList->compare)
(pArgu,pList->rear->dataPtr))
int result,cnt=0;
*pPre=NULL;
*pLoc=pList->head;
if(pList->count==0)
return 0;
if(COMPARE_LAST>0)
{
*pPre=pList->rear;
*pLoc=NULL;
return 0;
}
while((result=COMPARE)>0)
{
*pPre=*pLoc;
*pLoc=(*pLoc)->link;
cnt++;
}
if(result==0)
return ++cnt;
else
return 0;
}
static
int _insert(LIST *pList,NODE *pPre,int dataInPtr)
{
NODE *pNew;
if(!(pNew=(NODE *)malloc(sizeof(NODE))))
return 0;
pNew->dataPtr=dataInPtr;
pNew->link=NULL;
if(pPre==NULL)
{
pNew->link=pList->head;
pList->head=pNew;
if(pList->count==0)
pList->rear=pNew;
}
else
{
pNew->link=pPre->link;
pPre->link=pNew;
if(pNew->link==NULL)
pList->rear=pNew;
}
(pList->count)++;
return 1;
}
int
addNode(LIST *pList,int dataInPtr)
{
int found,success,i,n;
NODE *p,*pPre,*pLoc;
found=_search(pList,&pPre,&pLoc,dataInPtr);
if(found)
return(+1);
success=_insert(pList,pPre,dataInPtr);
/*display element*/
p=pList->head;
n=pList->count;
for(i=0;i<n;i++)
{
printf("%d\t",p->dataPtr);
p=p->link;
}
if(!success)
return(-1);
return(0);
}
void
_delete(LIST *pList,NODE *pPre,NODE *pLoc)
{
int dataOutPtr;
dataOutPtr=pLoc->dataPtr;
if(pPre==NULL)
pList->head=pLoc->link;
else
pPre->link=pLoc->link;
if(pLoc->link==NULL)
pList->rear=pPre;
(pList->count)--;
free(pLoc);
return;
}
int
removeNode(LIST *pList,int keyPtr)
{
int found,n,i;
NODE *pPre;
NODE *pLoc;
NODE *p;
found=_search(pList,&pPre,&pLoc,keyPtr);
if(found)
{
_delete(pList,pPre,pLoc);
p=pList->head;
n=pList->count;
for(i=0;i<n;i++)
{
printf("%d\t",p->dataPtr);
p=p->link;
}
}
printf("\n");
return found;
}
int
searchList(LIST *pList,int pArgu)
{
int found,pDataOut;
NODE *pPre;
NODE *pLoc;
found=_search(pList,&pPre,&pLoc,pArgu);
if(found)
pDataOut=pLoc->dataPtr;
else
pDataOut=NULL;
return found;
}
int
traverse(LIST *pList,int fromwhere)
{
int dataOut;
int success;
if(fromwhere==0)
{
if(pList->count==0)
success=0;
else
{
pList->pos=pList->head;
dataOut=pList->pos->dataPtr;
printf("%d\t",dataOut);
success=1;
}
}
else
{
if(pList->pos->link==NULL)
success=0;
else
{
pList->pos=pList->pos->link;
dataOut=pList->pos->dataPtr;
printf("%d\t",dataOut);
success=1;
}
}
return success;
}
LIST
* destroyList(LIST *pList)
{
NODE *deletePtr;
if(pList)
{
while(pList->count>0)
{
free(pList->head->dataPtr);
deletePtr=pList->head;
pList->head=pList->head->link;
pList->count--;
free(deletePtr);
}
free(pList);
}
return NULL;
}
void
main()
{
int ch=1;
int item,deleteKey,dataOutPtr;
LIST *pList,*cList;
int opt,n,i,cnt,found,dataOut;
NODE *p,*pre,*loc;
clrscr();
printf("Welcome to exploring linked
lists.\n");
printf("........MENU.......\n");
printf(" 1: CREATELIST \n");
printf(" 2: ADD NODE.\n");
printf(" 3: REMOVE NODE \n");
printf(" 4: LIST COUNT \n");
printf(" 5: SEARCH LIST \n");
printf(" 6: TRAVERSE/DISPLAY \n");
printf(" 7: REVERSE LIST \n");
printf(" 8: DESTROY LIST \n");
printf(" 9: Quit\n");
while(ch)
{
printf("\nEnter the choice
:-> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
pList=createList(cmp);
printf("pList
created\n");
break;
case 2:
printf("Enter the data
: ");
scanf("%d",&item);
addNode(pList,item);
printf("\n");
break;
case 3:
printf("Enter the data
: ");
scanf("%d",&deleteKey);
removeNode(pList,deleteKey);
break;
case 4:
p=pList->head;
cnt=0;
while(p!=NULL)
{
cnt++;
p=p->link;
}
printf("No. of
elements in a list is : %d\n",cnt);
break;
case 5:
printf("Enter the item
: ");
scanf("%d",&item);
found=searchList(pList,item);
if(found)
printf("Element
found at position %d\n",found);
else
printf("Element
not found\n");
break;
case 6:
n=pList->count;
for(i=0;i<n;i++)
{
traverse(pList,i);
}
printf("\n");
break;
case 7:
p=pList->rear;
while(p!=NULL)
{
found=_search(pList,&pre,&loc,p->dataPtr);
printf("%d\t",loc->dataPtr);
p=pre;
}
printf("\n");
break;
case 8:
destroyList(pList);
printf("pList is
destroyed\n");
break;
case 9:
exit(0);
break;
default:
printf("Entered wrong
choice!\n");
break;
}
}
}
OUTPUT:
Welcome
to exploring linked lists.
........MENU.......
1: CREATELIST
2: ADD NODE.
3: REMOVE NODE
4: LIST COUNT
5: SEARCH LIST
6: TRAVERSE/DISPLAY
7: REVERSE LIST
8: DESTROY LIST
9: QUIT.
Enter
the choice :-> 1
pList
created
Enter
the choice :-> 2
Enter
the data : 23
23
Enter
the choice :-> 2
Enter
the data : 12
12 23
Enter
the choice :-> 2
Enter
the data : 56
12 23
56
Enter
the choice :-> 2
Enter
the data : 45
12 23
45 56
Enter
the choice :-> 2
Enter
the data : 85
12 23
45 56 85
Enter
the choice :-> 2
Enter
the data : 41
12 23
41 45 56
85
Enter
the choice :-> 3
Enter
the data : 12
23 41
45 56 85
Enter
the choice :-> 3
Enter
the data : 41
23 45
56 85
Enter
the choice :-> 4
No.
of elements in a list is : 4
Enter
the choice :-> 5
Enter
the item : 56
Element
found at position 3
Enter
the choice :-> 5
Enter
the item : 12
Element
not found
Enter
the choice :-> 21
Entered
wrong choice!
Enter
the choice :-> 6
23 45
56 85
Enter
the choice :-> 7
85 56
45 23
Enter
the choice :-> 8
pList
is destroyed
Enter
the choice :-> 10
#include<conio.h>
typedef
struct node
{
int dataPtr;
struct node *link;
}NODE;
typedef
struct
{
int count;
NODE *pos;
NODE *head;
NODE *rear;
int (*compare)(int argu1,int argu2);
}LIST;
int
cmp(int argu1,int argu2)
{
if(argu1==argu2)
return(0);
if(argu1>argu2)
return(+1);
else
return(-1);
}
LIST
*createList(int (*compare) (int argu1,int argu2))
{
/*Local
Declaration*/
LIST *list;
if(list)
{
list->head=NULL;
list->pos=NULL;
list->rear=NULL;
list->count=0;
list->compare=compare;
}
return list;
}
int
_search(LIST *pList,NODE **pPre,NODE **pLoc,int pArgu)
{
#define COMPARE \ (((*pList->compare)
(pArgu,(*pLoc)->dataPtr)))
#define COMPARE_LAST \ ((*pList->compare)
(pArgu,pList->rear->dataPtr))
int result,cnt=0;
*pPre=NULL;
*pLoc=pList->head;
if(pList->count==0)
return 0;
if(COMPARE_LAST>0)
{
*pPre=pList->rear;
*pLoc=NULL;
return 0;
}
while((result=COMPARE)>0)
{
*pPre=*pLoc;
*pLoc=(*pLoc)->link;
cnt++;
}
if(result==0)
return ++cnt;
else
return 0;
}
static
int _insert(LIST *pList,NODE *pPre,int dataInPtr)
{
NODE *pNew;
if(!(pNew=(NODE *)malloc(sizeof(NODE))))
return 0;
pNew->dataPtr=dataInPtr;
pNew->link=NULL;
if(pPre==NULL)
{
pNew->link=pList->head;
pList->head=pNew;
if(pList->count==0)
pList->rear=pNew;
}
else
{
pNew->link=pPre->link;
pPre->link=pNew;
if(pNew->link==NULL)
pList->rear=pNew;
}
(pList->count)++;
return 1;
}
int
addNode(LIST *pList,int dataInPtr)
{
int found,success,i,n;
NODE *p,*pPre,*pLoc;
found=_search(pList,&pPre,&pLoc,dataInPtr);
if(found)
return(+1);
success=_insert(pList,pPre,dataInPtr);
/*display element*/
p=pList->head;
n=pList->count;
for(i=0;i<n;i++)
{
printf("%d\t",p->dataPtr);
p=p->link;
}
if(!success)
return(-1);
return(0);
}
void
_delete(LIST *pList,NODE *pPre,NODE *pLoc)
{
int dataOutPtr;
dataOutPtr=pLoc->dataPtr;
if(pPre==NULL)
pList->head=pLoc->link;
else
pPre->link=pLoc->link;
if(pLoc->link==NULL)
pList->rear=pPre;
(pList->count)--;
free(pLoc);
return;
}
int
removeNode(LIST *pList,int keyPtr)
{
int found,n,i;
NODE *pPre;
NODE *pLoc;
NODE *p;
found=_search(pList,&pPre,&pLoc,keyPtr);
if(found)
{
_delete(pList,pPre,pLoc);
p=pList->head;
n=pList->count;
for(i=0;i<n;i++)
{
printf("%d\t",p->dataPtr);
p=p->link;
}
}
printf("\n");
return found;
}
int
searchList(LIST *pList,int pArgu)
{
int found,pDataOut;
NODE *pPre;
NODE *pLoc;
found=_search(pList,&pPre,&pLoc,pArgu);
if(found)
pDataOut=pLoc->dataPtr;
else
pDataOut=NULL;
return found;
}
int
traverse(LIST *pList,int fromwhere)
{
int dataOut;
int success;
if(fromwhere==0)
{
if(pList->count==0)
success=0;
else
{
pList->pos=pList->head;
dataOut=pList->pos->dataPtr;
printf("%d\t",dataOut);
success=1;
}
}
else
{
if(pList->pos->link==NULL)
success=0;
else
{
pList->pos=pList->pos->link;
dataOut=pList->pos->dataPtr;
printf("%d\t",dataOut);
success=1;
}
}
return success;
}
LIST
* destroyList(LIST *pList)
{
NODE *deletePtr;
if(pList)
{
while(pList->count>0)
{
free(pList->head->dataPtr);
deletePtr=pList->head;
pList->head=pList->head->link;
pList->count--;
free(deletePtr);
}
free(pList);
}
return NULL;
}
void
main()
{
int ch=1;
int item,deleteKey,dataOutPtr;
LIST *pList,*cList;
int opt,n,i,cnt,found,dataOut;
NODE *p,*pre,*loc;
clrscr();
printf("Welcome to exploring linked
lists.\n");
printf("........MENU.......\n");
printf(" 1: CREATELIST \n");
printf(" 2: ADD NODE.\n");
printf(" 3: REMOVE NODE \n");
printf(" 4: LIST COUNT \n");
printf(" 5: SEARCH LIST \n");
printf(" 6: TRAVERSE/DISPLAY \n");
printf(" 7: REVERSE LIST \n");
printf(" 8: DESTROY LIST \n");
printf(" 9: Quit\n");
while(ch)
{
printf("\nEnter the choice
:-> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
pList=createList(cmp);
printf("pList
created\n");
break;
case 2:
printf("Enter the data
: ");
scanf("%d",&item);
addNode(pList,item);
printf("\n");
break;
case 3:
printf("Enter the data
: ");
scanf("%d",&deleteKey);
removeNode(pList,deleteKey);
break;
case 4:
p=pList->head;
cnt=0;
while(p!=NULL)
{
cnt++;
p=p->link;
}
printf("No. of
elements in a list is : %d\n",cnt);
break;
case 5:
printf("Enter the item
: ");
scanf("%d",&item);
found=searchList(pList,item);
if(found)
printf("Element
found at position %d\n",found);
else
printf("Element
not found\n");
break;
case 6:
n=pList->count;
for(i=0;i<n;i++)
{
traverse(pList,i);
}
printf("\n");
break;
case 7:
p=pList->rear;
while(p!=NULL)
{
found=_search(pList,&pre,&loc,p->dataPtr);
printf("%d\t",loc->dataPtr);
p=pre;
}
printf("\n");
break;
case 8:
destroyList(pList);
printf("pList is
destroyed\n");
break;
case 9:
exit(0);
break;
default:
printf("Entered wrong
choice!\n");
break;
}
}
}
OUTPUT:
Welcome
to exploring linked lists.
........MENU.......
1: CREATELIST
2: ADD NODE.
3: REMOVE NODE
4: LIST COUNT
5: SEARCH LIST
6: TRAVERSE/DISPLAY
7: REVERSE LIST
8: DESTROY LIST
9: QUIT.
Enter
the choice :-> 1
pList
created
Enter
the choice :-> 2
Enter
the data : 23
23
Enter
the choice :-> 2
Enter
the data : 12
12 23
Enter
the choice :-> 2
Enter
the data : 56
12 23
56
Enter
the choice :-> 2
Enter
the data : 45
12 23
45 56
Enter
the choice :-> 2
Enter
the data : 85
12 23
45 56 85
Enter
the choice :-> 2
Enter
the data : 41
12 23
41 45 56
85
Enter
the choice :-> 3
Enter
the data : 12
23 41
45 56 85
Enter
the choice :-> 3
Enter
the data : 41
23 45
56 85
Enter
the choice :-> 4
No.
of elements in a list is : 4
Enter
the choice :-> 5
Enter
the item : 56
Element
found at position 3
Enter
the choice :-> 5
Enter
the item : 12
Element
not found
Enter
the choice :-> 21
Entered
wrong choice!
Enter
the choice :-> 6
23 45
56 85
Enter
the choice :-> 7
85 56
45 23
Enter
the choice :-> 8
pList
is destroyed
Enter
the choice :-> 10
|
Computer Science :: 11th/12th, Computer Engineering :: Degree/Diploma, Coding Concept ( C, C++ & JAVA Programming, JDBC Concept, DBMS - SQL Concept, Data Structure, Communication & Network Concept.
SINGLE LINKED LIST
Subscribe to:
Posts (Atom)
Python Code Loop Program-1
#Chapter -7 Qno - 2 num=4; while(num<10): print("Number is %d"%num); num=num+1; Output Number is 4 Number is 5 Number ...
-
COMPUTER TERMINOLOGY 1) TCP/IP - Transmission Control Protocol/ Internet Protocol. 2) HTTP...
-
Programmingway@Eklavya Technosys!: PHP Exercise Solutions : PHP Exercise Solutions 1) Fill in the blanks a) The extension of a PHP cod...
-
EduTech Digital Study Contents | Ashok Sir Class - 09 IT 402 Exercises Questions Class - 10 | IIT | Introductory to IIT | ...
-
https://makecode.com/_3VxaMo6c3YPp Code 1) What is Make Code Arcade? Answer: Make Code Arcade is a Block based coding. 2) Writ...
-
Check whether the number is negative or positive after taking a number as input from the user. TextWindow . WriteLine ( "Ente...
No comments:
Post a Comment