You are on page 1of 41

Module III

Linear Data Structures and their Linked Storage Representation


3.1 Linked List

Linked list is the collection of inter connected nodes with a head node representing the first node
and a tail node representing the last node of it. A node can be viewed as a block consisting of
two major fields : a data field and a pointer field. The pointer field is used to interconnect nodes
of a list. Below diagram depicts the linked list setup.

Tail

NULL

3.2 Linked list types

There are few variants of linked lists. Below diagram shows a brief classification of Linked
Lists.
Linked List

Singly LL Circular LL Doubly LL

Singly Circular LL Doubly Circular LL

Regarding these variants of LL, explanations and examples are given in subsequent sections of
the notes.

3.2 Linked List Representation

A simple representation of singly linked list would look like the below:
struct node {
int data;
struct node *next; };
struct node *newnode;
With the above declaration we can create a node of the pattern below:
data *next

newnode
Initially, the pointer field of the first node in singly linked list would contain NULL value.
Instruction that makes it is: newnode->next = NULL;
Note: Structure variables’ fields of self referential type are accessed through -> (arrow
operator)
Now, let us try to understand few conventions w.r.t linked lists. Consider below given list.

10 1008h 20 1016h 30 1024h 40 NULL

head (1000h) (1008h) (1016h) tail (1024h)


In this list, head node contains data = 10 and tail node contains data = 40. This list has 4 nodes
that are interconnected. Head node is connected to the next node since the pointer value contains
the address of next node. Tail node is not linked to any node since its pointer value is NULL.
struct node *head, *tail, *newnode; // variables declared of type struct node
head = (struct node *) malloc (1 * sizeof(struct node));
This instruction creates one node of type struct. Memory gets allocated using DMA function
malloc(). Created node in the above instruction is referred to as “head”. It can be any other name
as well.
head->data = 10; // assigns head node’s data field with 10
head->next = NULL; //assigning pointer value to NULL
10 NULL
head; tail
tail = head; // assigning the reference of head to tail. This means both head and tail are referring
to the same node.
Now, let us add a new node to this list.
newnode = (struct node *) malloc (1 * sizeof(struct node));
newnode->data = 20; newnode->next = NULL;
10 NULL 10 NULL

Head; tail newnode


The above nodes can be connected using the following instructions:
head->next = newnode; // assigning head node’s pointer value with address of newnode

10 1008h 20 NULL

head; tail (1000h) newnode (1008h)


Now, if we execute the instruction, tail = newnode; tail & newnode both refer to the node with
value 20.
In this way, linked lists can be created.
3.3 Operations on Linked Lists
Major operations performed on linked lists are inserting a node, deleting a node, searching a
node and traversing the list to display all of its nodes.
a) Inserting a node: Node can be inserted at the beginning, at the end or anywhere in the
linked list
10 1008h 20 1016h 30 1024h 40 NULL
head (1000h) (1008h) (1016h) tail (1024h)
Consider the above list. Suppose we wish to insert a node at the beginning, following set of
instruction are to be executed.
newnode = (struct node *) malloc (1 * sizeof(struct node));
newnode->data = 50; newnode->next = head; 50 1000h
head = newnode;

10 1008h 20 1016h 30 1024h 40 NULL

(1000h) (1008h) (1016h) tail (1024h)

50 1000h
head; newnode
Suppose we wish to insert a node at the end, following set of instruction are to be executed.

10 1008h 20 1016h 30 1024h 40 1032h


head (1000h) (1008h) (1016h) tail (1024h)

newnode = (struct node *) malloc (1 * sizeof(struct node)); 80 NULL


newnode->data = 80; newnode->next = NULL; 80 NULL (1032h)
Take a temporary variable of type struct node, traverse the list until you reach the last node (i.e
tail )
while(temp->next !=NULL)
{
temp=temp->next;
}
temp->next = newnode;
tail = newnode;
Now let us see how to insert a node at a particular position in the list. For this, we need to ask
user to specify after which node in the list he would want to insert. Call it as “key” node. This
can be realized as below:
temp
10 1008h 20 1016h 30 1040h 40 1032h

head (1000h) (1008h) (1016h) tail (1024h)

80 NULL
Key = 30 100 1032h

Newnode (1040h) (1032h)


Take a temporary variable of type struct node, traverse the list until you reach the desired node
(i.e 30)
while(temp->data !=key)
100 NULL
{
temp=temp->next; newnode (1040h)
}
newnode->next = temp->next;
temp->next = newnode;
b) Deleting a node: A node in the list can be deleted using the standard function free ( ). Let
us see an example to understand delete operations on linked list.
10 1008h 20 1024h 30 NULL 40 NULL
head (1000h) temp2 (1008h) temp(1016h) tail (1024h)
Let us try to delete node with 30 data value.
Take two temporary variables and position them to key node and the previous node of it.
Now, key =30.
While(temp->data!=key)
{
temp=temp->next;
}
While(temp2->next!=key)
{
temp2=temp2->next;
}
temp2->next = temp->next;
temp->next = NULL;
free(temp);

Below C program creates a singly linked list and performs few operations like insert, delete,
display, search, insert and delete at specific positions in the list.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *head, *last,*temp, *temp2;
void create();
void delet();
void dele_spec();
void display();
void spec_ins();
int search(int);
int num,con=1,ch,key,f;
void main()
{
clrscr();
head=NULL;
printf("1.Create\n2.Delete\n3.Delete Specific\n4.Insert After Specific node\n5.Display\n6.
Search\n7.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: dele_spec();
break;
case 4: spec_ins();
break;
case 5: display();
break;
case 6: printf("Enter the node data to be searched\n");
scanf("%d",&key);
f=search(key);
if(f==1) printf("NODE FOUND\n");
else printf("NODE NOT FOUND\n");
break;
case 7: exit(0);
}
}
}
void create()
{
if(head==NULL)
{
head = (struct node*)malloc(1*sizeof(struct node));
head->data=num;
head->ptr=NULL;
last=head;
}
else
{
temp= (struct node*)malloc(1*sizeof(struct node));
temp->data=num;
last->ptr=temp;
temp->ptr=NULL;
last=temp;
}
}
void delet()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head->ptr;
free(head);
head=temp;
}
}
void dele_spec()
{
int key;
printf("Enter the node to be deleted\n");
scanf("%d",&key);
temp=head;
temp2=head;
while(temp->data!=key)
{
temp=temp->ptr;
}
while(temp2->ptr!=temp)
{
temp2=temp2->ptr;
}
if(temp->ptr==NULL)
{
temp2->ptr=NULL;
free(temp);
last=temp2;
}
else
{
temp2->ptr=temp->ptr;
free(temp);
}
}
void spec_ins()
{
int key;
printf("Enter the node info next to which to be inserted\n");
scanf("%d",&key);
printf("Enter data for newnode\n");
scanf("%d",&num);
temp2= (struct node*)malloc(1*sizeof(struct node));
temp2->data=num;
temp=head;
while(temp->data!=key)
{
temp=temp->ptr;
}
temp2->ptr=temp->ptr;
temp->ptr=temp2;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
}
int search(int key)
{
int flag=0;
temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;
return flag;
}
else
temp=temp->ptr;
}
return flag;
}
Advantages of Linked Lists
 They are a dynamic in nature which allocates the memory when required.
 Insertion and deletion operations can be easily implemented.
 Stacks and queues can be easily executed.
 Linked List reduces the access time.
Disadvantages of Linked Lists
 The memory is wasted as pointers require extra memory for storage.
 No element can be accessed randomly; it has to access each node sequentially.
 Reverse Traversing is difficult in linked list.

Applications of Linked Lists


 Linked lists are used to implement stacks, queues, graphs, etc.
 Linked lists let you insert elements at the beginning and end of the list.
 In Linked Lists we don’t need to know the size in advance.

3.4 Types of Linked Lists


Singly Linked List : Singly linked lists contain nodes which have a data part as well as an
address part i.e. next, which points to the next node in sequence of nodes. The operations we can
perform on singly linked lists are insertion, deletion and traversal.

Doubly Linked List : In a doubly linked list, each node contains two links the first link points to
the previous node and the next link points to the next node in the sequence.

Circular Linked List : In the circular linked list the last node of the list contains the address of
the first node and forms a circular chain.

3.5 Header Linked List


Header linked list is similar to Singly linked List but the difference is that in this type of list,
there exists a special node called “head”, which either contains information regarding total nodes
in the list or the address of the last node in the whole list.
C Program to implement header linked list is shown below
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *head, *last,*temp, *newnode;
void create();
void delet();
void display();
int num,count=0,ch;
void main()
{
clrscr();
head=NULL;
printf("1.Create\n2.Delete\n3.Display\n4.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(head==NULL)
{
head = (struct node*)malloc(1*sizeof(struct node));
head->data=count;
head->ptr=NULL;
last=head;
}
printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void create()
{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
newnode->ptr=NULL;
last->ptr=newnode;
last=newnode;
count++;
head->data=count;
}
void delet()
{
if(head->data==0)
{
printf("List is empty\n");
}
else
{
temp=head;
while(temp->ptr!=last)
{
temp=temp->ptr;
}
free(last);
last =temp;
last->ptr=NULL;
}
count--;
head->data=count;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head->ptr;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
printf("\nHeader Information:%d\n",head->data);
}
3.5 Linked implementation of Stack
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
3.6 Merging of two lists
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_list1();
void insert_list2();
void display();
void merge_list();
struct node *list1, *list2,*list3,*temp1,*temp2,*temp3,*temp4;
void main()
{
int ch;
clrscr();
printf("1 for insert in first linklist\n");
printf("2 for insert in second linklist\n");
printf("3 for display first & second linklist\n");
printf("4 for merge linklist\n");
printf("5 for exit\n");
while(1)
{
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_list1();
break;
case 2: insert_list2();
break;
case 3: display();
break;
case 4: merge_list();
break;
case 5: exit(0);
}
}
}

void merge_list()
{
temp1=list1;
temp2=list2;
while(temp1!=NULL || temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
if(list3==NULL)
{
list3=temp3;
temp4=temp3;
}
if(temp1->data<temp2->data)
{
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
else
{
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
if(temp2==NULL)
{
while(temp1!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
break;
}
if(temp1==NULL)
{
while(temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
break;
}
}
temp3->next=NULL;
}

void insert_list1()
{
struct node *new;
if(list1==NULL)
{
list1=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list1->data);
list1->next=NULL;
temp1=list1;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp1->next=new;
temp1=temp1->next;
}
}

void insert_list2()
{
struct node *new;
if(list2==NULL)
{
list2=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list2->data);
list2->next=NULL;
temp2=list2;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp2->next=new;
temp2=temp2->next;
}
}

void display()
{
temp1=list1;
temp2=list2;
printf("\nThe Information of First linklist:");
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
printf("\nThe Information of Second linklist:");
while(temp2!=NULL)
{
printf("%d ",temp2->data);
temp2=temp2->next;
}
temp3=list3;
printf("\nMerged List:");
while(temp3!=NULL)
{
printf("%d ",temp3->data);
temp3=temp3->next;
}
}
3.7 Reversing a Singly Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_list();
void display();
void revers();
struct node *head,*tail,*temp,*temp1;
void main()
{
int ch;
clrscr();
printf("1 insert in list\n");
printf("2 Reverse and display list\n");
printf("3.Exit\n");
while(1)
{
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_list();
break;
case 2: revers();
break;
case 3: exit(0);
}
}
}
void revers()
{
temp=tail;
temp1=head;
while(temp!=head)
{
temp1=head;
while(temp1->next!=temp)
{
temp1=temp1->next;
}
temp->next=temp1;
temp=temp1;
}
temp=tail;
head->next=NULL;
printf("Reversed list: ");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void insert_list()
{
struct node *new;
if(head==NULL)
{
head=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&head->data);
head->next=NULL;
tail=temp=head;

}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp->next=new;
temp=temp->next;
tail=temp;
}
temp1=head;
printf("Created list: ");
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
}
3.8 Singly Circular Linked List Operations
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *newnode, *front,*rear, *temp;

void create();
void display();
void delet();
int num,ch;
void main()
{
front=rear=NULL;
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);

}
}
}
void create()
{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
if(rear==NULL)
front=rear=newnode;
else
{
rear->ptr=newnode;
rear=newnode;
}
rear->ptr=front;
}
void delet()
{
if(front==NULL)
printf("List is empty\n");
else if(front==rear)
{
printf("Element deleted is %d",front->data);
front=NULL;
}
else
{
temp=front;
printf("Deleted element is %d",temp->data);
temp=temp->ptr;
free(front);
front=temp;
rear->ptr=front;
}
}
void display()
{
if(front==NULL)
{
printf("List empty\n");
}
else
{
temp=front;
while(temp!=rear)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
printf("%d\n",temp->data);
}
3.9 Adding and evaluating Polynomials using Circular Linked List
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int coeff;
int expo;
struct node *ptr;
};
struct node *head1,*head2,*head3, *temp,*temp1,*temp2,*temp3,*list1,*list2,*list3;
struct node *dummy1,*dummy2;
void create_poly1(int , int);
void create_poly2(int , int);
void display();
void add_poly();
void eval_poly(int );
int n,ch;
int c,e,i;
void main()
{
int x;
list1=list2=NULL;
printf("1.Create first polynomial\n2.Create Second Polynomial\n3.Display both the
polynomials\n");
printf("4.Add Polynomials\n5.Evaluate a Polynomial\n6.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Enter coefficient & power of each term\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&e);
create_poly1(c,e);
}
break;
case 2: printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Enter coefficient & power of each term\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&e);
create_poly2(c,e);
}
break;
case 3: display();
break;
case 4: add_poly();
break;
case 5:printf("Enter the value for x\n");
scanf("%d",&x);
eval_poly(x);
break;
case 6:exit(0);
}
}
}
void create_poly1(int c, int e)
{
dummy1=(struct node*)malloc(1*sizeof(struct node));
dummy1->coeff=0;
dummy1->expo=0;
dummy1->ptr=list1;
if(list1==NULL)
{
list1=(struct node*)malloc(1*sizeof(struct node));
list1->coeff=c;
list1->expo=e;
list1->ptr=list1;
head1=list1;
head1->ptr=dummy1;
}
else
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=c;
temp->expo=e;
head1->ptr=temp;
temp->ptr=dummy1;
head1=temp;

}
}

void create_poly2(int c, int e)


{
dummy2=(struct node*)malloc(1*sizeof(struct node));
dummy2->coeff=0;
dummy2->expo=0;
dummy2->ptr=list2;
if(list2==NULL)
{
list2=(struct node*)malloc(1*sizeof(struct node));
list2->coeff=c;
list2->expo=e;
list2->ptr=list2;
head2=list2;
head2->ptr=dummy2;
}
else
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=c;
temp->expo=e;
head2->ptr=temp;
temp->ptr=dummy2;
head2=temp;
}
}
void add_poly()
{
temp1=list1;
temp2=list2;
while((temp1!=dummy1)&&(temp2!=dummy2))
{
temp=(struct node*)malloc(1*sizeof(struct node));
if(list3==NULL)
{
list3=temp;
head3=list3;
}
if(temp1->expo==temp2->expo)
{
temp->coeff=temp1->coeff+temp2->coeff;
temp->expo=temp1->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp1=temp1->ptr;
temp2=temp2->ptr;
}
else if(temp1->expo>temp2->expo)
{
temp->coeff=temp1->coeff;
temp->expo=temp1->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp1=temp1->ptr;
}
else
{
temp->coeff=temp2->coeff;
temp->expo=temp2->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp2=temp2->ptr;
}
}
if(temp1==dummy1)
{
while(temp2!=dummy2)
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=temp2->coeff;
temp->expo=temp2->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp2=temp2->ptr;
}
}
if(temp2==dummy2)
{
while(temp1!=dummy1)
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=temp1->coeff;
temp->expo=temp1->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp1=temp1->ptr;
}
}
}

void display()
{
temp1=list1;
temp2=list2;
temp3=list3;
printf("\nPOLYNOMIAL 1:");
while(temp1!=dummy1)
{
printf("%dX^%d+",temp1->coeff,temp1->expo);
temp1=temp1->ptr;
}
printf("\b ");
printf("\nPOLYNOMIAL 2:");
while(temp2!=dummy2)
{
printf("%dX^%d+",temp2->coeff,temp2->expo);
temp2=temp2->ptr;
}
printf("\b ");
printf("\n\nSUM OF POLYNOMIALS:\n");
while(temp3->ptr!=list3)
{
printf("%dX^%d+",temp3->coeff,temp3->expo);
temp3=temp3->ptr;
}
printf("%dX^%d\n",temp3->coeff,temp3->expo);
}

void eval_poly(int x)
{
int result=0;
temp1=list1;
temp2=list2;
while(temp1!=dummy1)
{
result+=(temp1->coeff)*pow(x,temp1->expo);
temp1=temp1->ptr;
}
printf("Polynomial 1 Evaluation:%d\n",result);
result=0;
while(temp2!=dummy2)
{
result+=(temp2->coeff)*pow(x,temp2->expo);
temp2=temp2->ptr;
}
printf("Polynomial 2 Evaluation:%d\n",result);
}
3.10 Doubly Linked list operations
Below shown C program demonstrates basic operations on Doubly Linked List
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct stud
{
long long int ph;
int sem;
char name[15],usn[15],brnch[8];
struct stud *next;
}*head=NULL,*tail=NULL,*temp=NULL,*temp1;
void create(long long int n,int s,char na[20],char u[15],char b[5])
{
if(head==NULL)
{
head=(struct stud*)malloc(1*sizeof(struct stud));
head->ph=n;
head->sem=s;
strcpy(head->name,na);
strcpy(head->usn,u);
strcpy(head->brnch,b);
head->next=NULL;
tail=head;
count++;
}
else
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=NULL;
tail->next=temp;
tail=temp;
count++;
}
}
void display()
{
temp1=head;
if(temp1==NULL)
{
printf("\nlist is empty\n");
}
else
{
printf("student details are as follows:\n");
while(temp1!=NULL)
{
printf("-----------------------\n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1-
>name,temp1->usn,temp1->brnch,temp1->sem,temp1->ph);
printf("-----------------------\n");
temp1=temp1->next;
}
printf("no. of nodes=%d\n",count);
}
}
void insert_head(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=head;
head=temp;
count++;
}
void insert_tail(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
tail->next=temp;
temp->next=NULL;
tail=temp;
count++;
}
void delete_head()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
else
{
head=head->next;
printf("deleted node is:\n");
printf("-----------------------\n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1-
>name,temp1->usn,temp1->brnch,temp1->sem,temp1->ph);
printf("-----------------------\n");
free(temp1);
count--;
}
}
void delete_tail()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
while(temp1->next!=tail)
{
temp1=temp1->next;
}
printf("deleted node is:\n");
printf("-----------------------\n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",tail->name,tail-
>usn,tail->brnch,tail->sem,tail->ph);
printf("-----------------------\n");
free(tail);
tail=temp1;
tail->next=NULL;
count--;
}
void main()
{
int choice;
long long int ph;
int sem;
char name[20],usn[15],brnch[5];
printf("--------MENU----------\n");
printf("1.create\n2.Insert from head\n3.Insert from tail\n4.Delete from head\5.Delete from
tail\n6.display\n7.exit\n");
printf("----------------------\n");
while(1)
{
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
create(ph,sem,name,usn,brnch);
break;
case 2: printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_head(ph,sem,name,usn,brnch);
break;
case 3: printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_tail(ph,sem,name,usn,brnch);
break;
case 4: delete_head();
break;
case 5: delete_tail();
break;
case 6: display();
break;
case 7: exit(0);
default:printf("invalid option\n");
}
}}

You might also like