unit - 5 linked list
unit - 5 linked list
Operations on a list:
insert : insert a new item into a list
delete : delete an item from list
List: list the items from the list
Retrieval : search for an element in the list
Singly Linked List The simplest kind of linked list is a singly linked list (or slist for short), which has one
link per node. This link points to the next node in the list, or to a null value or empty list if it is the final
node.
The following figures show the most common singly linked lists.
2
a)
Searching or find: This operation is used to find an element in a linked list. In the desired
element is found then we say operation is successful otherwise unsuccessful.
Concatenation: It is the process of appending second list to the end of the first list.
struct Node
{
int info;
struct Node *next;
};
typedef struct Node NodeType;
NodeType *head; //head is a pointer type structure variable This type of structure is called self-
referential structure.
Inserting Nodes:
To insert an element or a node in a linked list, the following three things to be done:
Allocating a node
Assigning a data to info field of the node
Adjusting a pointer and a new node may be inserted
At the beginning of the linked list
At the end of the linked list
At the specified position in a linked list
1. If(head==NULL) then
print “Void deletion” and exit
2. Store the address of first node in a temporary variable temp.
temp=head;
3. Set head to next of head.
head=head->next;
4. Free the memory reserved by temp variable.
free(temp);
5. End
An algorithm to delete a node after the given node in singly linked list:
let *head be the pointer to first node in the current list and *p be the pointer to
the node
after which we want to delete a new node.
1. if(p==NULL or p->next==NULL) then
print “deletion not possible and exit
2. set q=p->next
3. Set p->next=q->next;
4. free(q)
5. End
6
#include<stdio.h>
#include<conio.h>
#include<malloc.h> //for malloc function
#include<process.h> //fpr exit function
struct node
{int info;
struct node *next;
};
typedef struct node NodeType;
NodeType *head=NULL;
//head=NULL;
void insert_atfirst(int);
void insert_givenposition(int);
void insert_atend(int);
void delet_first();
void delet_last();
void delet_nthnode();
int main()
{
int choice;
int item;
7
//clrscr();
do
{
printf("\n manu for program:\n");
printf("1. insert first \n2.insert at given position \n3 insert at last \n 4:Delete first
node\n 5:delete last node\n6:delete nth node\n7:display nodes\n8Display
items\n10:exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter item to be inserted \n");
scanf("%d", &item);
insert_atfirst(item);
break;
case 2:
printf("Enter item to be inserted");
scanf("%d", &item);
insert_givenposition(item);
break;
case 3:
printf("Enter item to be inserted");
scanf("%d", &item);
insert_atend(item);
break;
case 4:
delet_first();
break;
case 5:
delet_last();
break;
case 6:
delet_nthnode();
break;
case 7:
info_sum();
break;
case 8:
count_nodes();
8
break;
case 9:
exit(1);
break;
default:
printf("invalid choice\n");
}
}while(choice<10);
return 0;
getch();
}
void insert_atfirst(int item)
{
NodeType *nnode;
nnode=(NodeType*)malloc(sizeof(NodeType));
nnode->info=item;
nnode->next=head;
head=nnode;
}
temp=temp->next;
9
nnode->next=NULL;
temp->next=nnode;
}
}
void info_sum()
{
NodeType *temp;
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
void insert_givenposition(int item)
{
NodeType *nnode;
NodeType *temp;
temp=head;
int p,i;
nnode=( NodeType *)malloc(sizeof(NodeType));
nnode->info=item;
if (head==NULL)
{
nnode->next=NULL;
head=nnode;
}
else
{
printf("Enter Position of a node at which you want to insert an new node\n");
scanf("%d",&p);
for(i=1;i<p-1;i++)
{
temp=temp->next;
}
nnode->next=temp->next;
temp->next=nnode;
}
10
}
void delet_first()
{
NodeType *temp;
if(head==NULL)
{
printf("Void deletion|n");
return;
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void delet_last()
{
NodeType *hold,*temp;
if(head==NULL)
{
printf("Void deletion|n");
return;
}
else if(head->next==NULL)
{
hold=head;
head=NULL;
free(hold);
}
else
{
temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
hold=temp->next;
temp->next=NULL;
free(hold);
11
}
}
void delet_nthnode()
{
NodeType *hold,*temp;
int pos, i;
if(head==NULL)
{
printf("Void deletion|n");
return;
}
else
{
temp=head;
printf("Enter position of node which node is to be deleted\n");
scanf("%d",&pos);
for(i=1;i<pos-1;i++)
{
temp=temp->next;
}
hold=temp->next;
temp->next=hold->next;
free(hold);
}
}
void count_nodes()
{
int cnt=0;
NodeType *temp;
temp=head;
while(temp!=NULL)
{
cnt++;
temp=temp->next;
}
printf("total nodes=%d",cnt);
}
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NodeType;
NodeType *top=0;
//top=0;
void push(int);
void pop();
void display();
int main()
{
printf("\n top is %d",top);
int choice, item;
do
{
printf("\n1.Push \n2.Pop \n3.Display\n4:Exit\n");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the data:\n");
scanf("%d",&item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
13
printf("invalid choice\n");
break;
}
}while(choice<5);
getch();
}
/**************push function*******************/
void push(int item)
{
NodeType *nnode;
int data;
nnode=( NodeType *)malloc(sizeof(NodeType));
if(top==0)
{
nnode->info=item;
nnode->next=NULL;
top=nnode;
}
else
{
nnode->info=item;
nnode->next=top;
top=nnode;
}
}
// pop function*
void pop()
{
NodeType *temp;
if(top==0)
{
printf("Stack contain no elements:\n");
return;
}
else
{
temp=top;
top=top->next;
14
Insert function:
let *rear and *front are pointers to the first node of the list initially and insertion of node
in linked.
15
printf("%d\t",temp->info);
temp=temp->next;
}
}
4. set newnode->next=head
5. set head->prev=newnode
6. set head=newnode
7. End
struct node
{
struct node *prev;
int info;
struct node *next;
}*head;
typedef struct node nodetype;
void create_list(int m);
void create_list(int m);
void addatbeg(int m);
void del(int m);
void display();
void addafter(int m,int po);
main()
{
int choice,n,m,po,i;
head=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
20
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is
inserted : ");
scanf("%d",&po);
addafter(m,po);
break;
case 4:
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
// exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
else
{
q=head;
while(q->next!=NULL)
{
q=q->next;
}
q->next=tmp;
tmp->prev=q;
}
}/*End of create_list()*/
q->next=tmp;
}/*End of addafter() */
void display()
{
struct node *q;
if(head==NULL)
{
printf("List is empty\n");
23
return;
}
q=head;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */