".C" file is available in downloadable format at :
(Open the link and click on the download arrow on the top right to download the file.)
Given Below is the code for Singly Linked List :
--------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
typedef struct link
{
int info;
struct link* next;
}node;
void create (node*);
node* del (node*);
void modify (node*);
node* mysearch( node*, int );
void display (node*);
void create (node* st)
{
int aftr,inf;
node* afternode;
node* n;
node* temp;
printf("\nHello!! So you wish to add a new node.\n\nEnter the node after which the insertion to be done:\t");
scanf("%d", &aftr);
afternode = mysearch(st,aftr);
if(afternode==NULL)
printf("\nElement DNE");
else
{
printf("\nFound %d\n", aftr);
if (afternode!=NULL)
{
printf("\nEnter the data for the new node:\t");
scanf("%d", &inf);
n = (node*)malloc(sizeof(node));
n->info=inf;
temp = afternode->next;
n->next = temp;
afternode->next = n;
}
}
}
node* del (node* st)
{
int inf;
node* l=st;
node* culprit;
printf("\nEnter the node data which you wish to delete:\t");
scanf("%d", &inf);
culprit = mysearch (st,inf);
if (culprit==NULL)
printf("\nThe element: %d doesn't exist.", inf);
else
{
if(culprit==st)
{
printf("\nThe data %d is at the starting of the list",inf);
st=culprit->next;
culprit->next=NULL;
free(culprit);
printf("\nSuccesfully DELETED the entry %d.\n", inf);
}
else
{
printf("\nFound %d", inf);
l->next=culprit->next;
culprit->next = NULL;
free(culprit);
printf("\nSuccesfully DELETED the entry: %d.\n",inf);
}
}
return st;
}
void modify (node* st)
{
int inf,newdata;
node* key;
printf("\nEnter the node data that needs to be modified:\t");
scanf("%d", &inf);
key= mysearch(st, inf);
if(key==NULL)
printf("\nElement to be modified i.e. %d Does Not Exist",inf);
else
{
printf("\nEnter new data:\t");
scanf("%d", &newdata);
key->info = newdata;
}
}
node* mysearch (node* p, int key_data)
{
while((p!=NULL)&&(p->info !=key_data))
p=p->next;
return p;
}
void display (node* st)
{
node* p = (node*)st;
if (p==NULL)
printf("\n------------EMPTY------------\n");
else
{
printf("---");
while(p!=NULL)
{
printf("| %d |---", p->info);
p = p->next;
}
}
}
void removeDuplicates(node *start)
{
node *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while(ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest of the elements */
while(ptr2->next != NULL)
{
/* If duplicate then delete it */
if(ptr1->info == ptr2->next->info)
{
/* sequence of steps is important here */
dup = ptr2->next;
ptr2->next = ptr2->next->next;
free(dup);
}
else /* This is tricky */
{
ptr2 = ptr2->next;
}
}
ptr1 = ptr1->next;
}
}
void main()
{
int i, n, choice, data, search_ele;
node* p;
node* key_ele;
node* start=NULL;
node* left= (node*)start;
printf("\nHow many nodes will there be???\t");
scanf("%d",&n);
if(n<0)
printf("INVALID");
if(n==0)
printf("\nNo node will be inserted");
else
{
for(i=0;i<n;i++)
{
p = (node*)malloc(sizeof(node));
printf("\nEnter data at Position %d:\t",i+1);
scanf("%d", &data);
p->info = data;
p->next = NULL;
if (start == NULL)
{
start=p;
}
else
{
left->next=p;
}
left=p;
printf("\n-----------| %d Inserted |-----------\n", data);
}
while(1)
{
printf("\n\n\n1. Add new node.\n2. Delete a node\n3. Modify a node.\n4. Search for a particular node.\n5. Display List.\n6. Delete duplicates.\n7. Exit program.\n\nEnter your choice:\t");
scanf("%d", &choice);
switch(choice)
{
case 1: create(start); printf("The modified list is:\n"); display(start); break;
case 2: start = del(start); printf("The modified list is:\n"); display(start); break;
case 3: modify (start); printf("The modified list is:\n"); display(start); break;
case 4: printf("Enter value to be searched:\t"); scanf("%d", &search_ele); key_ele = mysearch (start, search_ele); if (key_ele==NULL) printf("\n Could not find %d",search_ele); else printf("\nThe element is found with value %d with pointer %p in RAM.", search_ele, key_ele); break;
case 5: display(start); break;
case 6: removeDuplicates(start); break;
case 7: exit(0);
default: printf("\n\nHey!! Choose an option from the choices mentioned!!\n And stop testing my program!!");
}
}
}
}
--------------------------------------------------------------------
No comments:
Post a Comment