0% found this document useful (0 votes)
70 views

Linked Lists

This document contains code for implementing four different data structures using linked lists in C: 1) A basic linked list with functions for insertion, deletion, counting nodes, and traversal. 2) A linked stack with push and pop functions. 3) A linked queue with enqueue and dequeue functions. 4) A priority queue where nodes are inserted and deleted based on priority. Main functions include insertion, deletion, checking if empty, and traversal.

Uploaded by

Sourav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Linked Lists

This document contains code for implementing four different data structures using linked lists in C: 1) A basic linked list with functions for insertion, deletion, counting nodes, and traversal. 2) A linked stack with push and pop functions. 3) A linked queue with enqueue and dequeue functions. 4) A priority queue where nodes are inserted and deleted based on priority. Main functions include insertion, deletion, checking if empty, and traversal.

Uploaded by

Sourav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1)Linked List

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;

void append(int num)


{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}

void add( int num )


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{

left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;
}

void insert(int num)


{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}

int delete(int num)


{
struct node *temp, *prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==head)
{

head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{
prev=temp;
temp= temp->next;
}
}
return 0;
}

void display(struct node *r)


{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf("\n");
}

int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}

int main()
{
int i,num;
struct node *n;
head=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0){
printf("Enter only an Integer\n");
exit(0);
} else {
switch(i)
{
case 1: printf("Enter the number to insert : ");
scanf("%d",&num);
insert(num);
break;
case 2: if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3: printf("Size of the list is %d\n",count());
break;
case 4: if(head==NULL)
printf("List is Empty\n");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
printf("%d deleted successfully\n",num);
else
printf("%d not found in the list\n",num); }
break;
case 5: return 0;
default: printf("Invalid option\n");
} } }
return 0; }

2) Linked 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) Linked Queue
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}

void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
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 Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;

}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

4) Linked Priority Queue


#include<stdio.h>
#include<stdlib.h>
struct node
{
int priority;
int info;
struct node *link;
}*front=NULL;
void insert(int item, int item_priority);
int del();
void display();
int isEmpty();
main()
{
int choice,item,item_priority;
while(1)
{
printf(1.Insert\n);
printf(2.Delete\n);
printf(3.Display\n);
printf(4.Quit\n);
printf(Enter your choice : );
scanf(%d, &choice);
switch(choice)
{
case 1:
printf(Input the item to be added in the queue : );
scanf(%d,&item);
printf(Enter its priority : );

scanf(%d,&item_priority);
insert(item, item_priority);
break;
case 2:
printf(Deleted item is %d\n,del());
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf(Wrong choice\n);
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
void insert(int item,int item_priority)
{
struct node *tmp,*p;
tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf(Memory not available\n);
return;
}
tmp->info=item;
tmp->priority=item_priority;
/*Queue is empty or item to be added has priority more than first element*/
if( isEmpty() || item_priority < front->priority )
{
tmp->link=front;
front=tmp;
}
else
{
p = front;
while( p->link!=NULL && p->link->priority<=item_priority )
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
}/*End of insert()*/
int del()
{
struct node *tmp;
int item;
if( isEmpty() )

{
printf(Queue Underflow\n);
exit(1);
}
else
{
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
}
return item;
}/*End of del()*/
int isEmpty()
{
if( front == NULL )
return 1;
else
return 0;
}/*End of isEmpty()*/
void display()
{
struct node *ptr;
ptr=front;
if( isEmpty() )
printf(Queue is empty\n);
else
{ printf(Queue is :\n);
printf(Priority
Item\n);
while(ptr!=NULL)
{
printf(%5d
%5d\n,ptr->priority,ptr->info);
ptr=ptr->link;
}
}
}/

You might also like