You are on page 1of 18

ASSIGNMENT-8

NAME: Subhajit Manna

DEPT: CSE

ROLL NO: 25300119041

1. Write a program to implement ADT of linear linked list


a. Create
b. Display
c. Delete from beginning
d. Delete from last
e. Delete a node after any position
f. Delete from any position
g. Delete a node with respect to a value

Algorithm:

Create list:
Step 1: create a node
Step 2: temp->info=data
Step 3: temp->link=NULL
Step 4: if start==NULL then go to step 4.1 else go to step 4.2
Step 4.1: start=temp
Step 4.2: q=start
Step 4.3: while(q->link!=NULL)
Step 4.3.1: q=q->link
Step 4.4: q->link=temp
Display:
Step 1: if(start==NULL) then go to step 1.1
Step 1.1: print list is empty
Step 1.2: return
Step 2: q=start
Step 3: while(q!=NULL)
Step 3.1: print q->link
Step 3.2: q=q->link
Delete at beginning:
Step 1: if(start==NULL)
Step 1.1: print empty list
Step 1.2: return
Step 2: temp pointer to start
Step 3: start=start->link
Step 4: free(temp)
Delete at last:
Step 1: if(start==NULL) then go to next step else go to step 2
Step 1.1: print empty list
Step1.2: return
Step 2: temp pointer to start
Step 3: q pointer to start
Step 4: while(temp->link!=NULL)
Step 4.1: q=temp
Step 4.2= temp=temp->link
Step 5: if(temp==start) then go to step 5.1 else go to step 5.2
Step 5.1=start =NULL
Step 5.2: q->link=Null
Step 6: free(temp)
Delete after any position:
Step 1: set i=1
Step 2: q pointer to start
Step 3: for i=1 and i<pos
Step 3.1: q=q->link
Step 4: temp=q->lnk
Step 5: q->link=temp->link
Step 6: free(temp)
Delete any position:
Step 1: if(pos==1)
Step 1.1: temp pointer to start
Step 1.2: start->start->link
Step 1.3: free(temp)
Step 1.4: return
Step 2: q pointer to start
Step 3: read i
Step 4: for i=2 and i<pos
Step 4.1: q=q->link
Step 4.2: if(q==NULL)
Step 4.2.1: print there are less than pos elements
Step 4.2.2: return
Step 5: temp=q->link
Step 6: q->link=temp->link
Step 7: free(temp)
Delete w.r.t data:
Step 1: if(start->info==data)
Step 1.1: temp=start
Step 1.2: start=start->link
Step 1.3: free(temp)
Step 1.4: return
Step 2: q pointer to start
Step 3: while(q->link->link!=NULL)
Step 3.1: if(q->link->info==data)
Step 3.1.1: temp=q->link
Step 3.1.2: q->link=temp->link
Step 3.1.3: free(temp)
Step 3.1.4: return
Step 4: if(q->link->info==data)
Step 4.1: temp=q->link
Step 4.2: free(temp)
Step 4.3: q->link=NULL
Step 4.4: return
Step 5: print element data not found

Source code:

#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*start;
main()
{
int ch,i,n,m,pos;
while(1)
{
printf("1. Create\n");
printf("2. Display\n");
printf("3. Delete from beginning\n");
printf("4. Delete from last\n");
printf("5. Delete after any node\n");
printf("6. Delete from any node\n");
printf("7. Delete a node w.r.t a value\n");
printf("enter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
start=NULL;
printf("enter the total number of nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements: ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
display();
break;
case 3:
del_beg();
break;
case 4:
del_last();
break;
case 5:
printf("enter the position after which element is deleted: ");
scanf("%d",&pos);
del_after(pos);
break;
case 6:
printf("enter the position: ");
scanf("%d",&pos);
del_any(pos);
break;
case 7:
printf("enter the element: ");
scanf("%d",&m);
del_wrt_value(m);
break;
case 8:
exit(1);
default:
printf("wrong choice.");
}
}
}
create_list(int data)
{
struct node *temp, *q;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
display()
{
struct node *q;
if(start==NULL)
{
printf("list is empty\n");
return;
}
q=start;
printf("list is: \n");
while(q!=NULL)
{
printf("%d\n",q->info);
q=q->link;
}
printf("\n");
}
del_beg()
{
if(start==NULL)
{
printf("list is empty.\n");
return;
}
struct node *temp;
temp=start;
start=start->link;
free(temp);
}
del_last()
{
struct node *temp, *q;
if(start==NULL)
printf("list is empty.\n");
else
{
temp=start;
q=start;
while(temp->link!=NULL)
{
q=temp;
temp=temp->link;
}
if(temp==start)
start=NULL;
else
q->link=NULL;
free(temp);
}
}
del_after(int pos)
{
int i=1;
struct node *temp, *q;
q=start;
for(i=1;i<pos;i++)
q=q->link;
temp=q->link;
q->link=temp->link;
free(temp);
return;
}
del_any(int pos)
{
struct node *temp, *q;
if(pos==1)
{
temp=start;
start=start->link;
free(temp);
return;
}
q=start;
int i;
for(i=2;i<pos;i++)
{
q=q->link;
if(q==NULL)
{
printf("there are less than %d elements",pos);
return;
}
}
temp=q->link;
q->link=temp->link;
free(temp);
}
del_wrt_value(int data)
{
struct node *temp, *q;
if(start->info==data)
{
temp=start;
start=start->link;
free(temp);
return;
}
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
return;
}
q=q->link;
}
if(q->link->info==data)
{
temp=q->link;
free(temp);
q->link=NULL;
return;
}
printf("element %d not found\n",data);
}

Output:

1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 1
enter the total number of nodes: 10
enter the elements: 2
enter the elements: 8
enter the elements: 4
enter the elements: 3
enter the elements: 7
enter the elements: 9
enter the elements: 6
enter the elements: 12
enter the elements: 10
enter the elements: 5
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
2
8
4
3
7
9
6
12
10
5

1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 3
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
9
6
12
10
5
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 4
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
9
6
12
10

1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 5
enter the position after which element is deleted: 4
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
6
12
10

1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 6
enter the position: 6
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
6
10

1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 7
enter the element: 3
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
7
6
10

1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice:

2. Write a program to implement sorted linear linked list

Algorithm:
Create list:
Step 1: create a node
Step 2: temp->info=data
Step 3: temp->link=NULL
Step 4: if start==NULL then go to step 4.1 else go to step 4.2
Step 4.1: start=temp
Step 4.2: q=start
Step 4.3: while(q->link!=NULL)
Step 4.3.1: q=q->link
Step 4.4: q->link=temp
Display:
Step 1: if(start==NULL) then go to step 1.1
Step 1.1: print list is empty
Step 1.2: return
Step 2: q=start
Step 3: while(q!=NULL)
Step 3.1: print q->link
Step 3.2: q=q->link
Shorted list:
Step 1: read p
Step 2: q pointer to start
Step 3: while(q->link!=NULL)
Step 3.1.1: temp=q->link
Step 3.1.2: while(temp!=NULL)
Step 3.1.2.1: if(q->info>temp->info)
Step 3.1.2.1.1: p=q->link
Step 3.1.2.1.2: q->info=temp->info
Step 3.1.2.1.3: temp->info=p
Step 3.1.2.2: temp=temp->link
Step 3.1.3: q=q->link
Source code:
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*start;
main()
{
int ch,n,m,i,pos,total;
while(1)
{
printf("1. Create list\n");
printf("2. Display\n");
printf("3. Shorting list\n");
printf("4. Quit\n");
printf("enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
start=NULL;
printf("enter the total number of nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements: ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
display();
break;
case 3:
shorted_list();
break;
case 10:
exit(1);
default:
printf("wrong choice.\n");
}
}
}
create_list(int data)
{
struct node *temp, *q;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
display()
{
struct node *q;
if(start==NULL)
{
printf("list is empty\n");
return;
}
q=start;
printf("list is: \n");
while(q!=NULL)
{
printf("%d\n",q->info);
q=q->link;
}
printf("\n");
}
shorted_list()
{
struct node *temp, *q;
int p;
q=start;
while(q->link!=NULL)
{
temp=q->link;
while(temp!=NULL)
{
if(q->info>temp->info)
{
p=q->info;
q->info=temp->info;
temp->info=p;
}
temp=temp->link;
}
q=q->link;
}
}
Output:
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 1
enter the total number of nodes: 5
enter the elements: 45
enter the elements: 35
enter the elements: 65
enter the elements: 55
enter the elements: 25
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 2
list is:
45
35
65
55
25

1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 3
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 2
list is:
25
35
45
55
65

1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice:

You might also like