#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//#define NULL 0
struct node
{
int data;
struct node *next;
}*p;
delnode(int num)
{
struct node *A, *m;
temp=p;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==p)
{
p=temp->next;
free(temp);
return;
}
else
{
m->next=temp->next;
free(temp);
return;
}
}else
{
m=temp;
temp= temp->next;
}
}
printf(" ELEMENT %d NOT FOUND ", num);
}
else
{
temp->next=p;
p=temp;
}
}
void main()
{
int i;
p=NULL;
while(1) /* this is an indefinite loop */
{
printf(" 1.INSERT A NUMBER AT BEGINNING;");
printf(" 2.INSERT A NUMBER AT LAST:");
printf(" 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST:");
printf(" 4.PRINT THE ELEMENTS IN THE LIST :");
printf(" 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST ");
printf(" 6.DELETE A NODE IN THE LINKED LIST:");
printf(" 7.REVERSE A LINKED LIST :");
printf(" 8.GET OUT OF LINKED LIST (BYEE BYEE):");
printf(" PLEASE, ENTER THE NUMBER:");
scanf("%d",&i); /* ENTER A VALUE FOR SWITCH */
switch(i)
{
case 1:
{
int num;
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
addbeg(num);
break;
}
case 2:
{
int num;
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
append(num);
break;
}
case 3:
{
int num, loc,k;
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
printf("PLEASE ENTER THE LOCATION NUMBER :-");
scanf("%d",&loc);
addafter(num,loc);
break;
} case 4:
{
struct node *n;
printf("THE ELEMENTS IN THE LIST ARE : ");
display(n);
break;
}
case 5:
{
struct node *n;
display(n);
printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());
break;
} case 6:
{
int num;
printf(" PLEASE ENTER A NUMBER FROM THE LIST :");
scanf("%d",&num);
delnode(num);
break;
}
case 7:
{
reverse(p);
display(p);
break;
}
case 8:
{
exit();
}
}/* end if switch */
}/* end of while */
}/* end of main */
OUTPUT
MENU
#include<stdio.h>
#include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
} ;
struct node *top;
void main()
{
void push(int);
void display();
int wish, num,will,a;
wish = 1;
top = NULL;
clrscr();
printf("Program for Stack as Linked List demo..");
while(wish == 1)
{
printf("Main Menu 1.Enter data in stack 2.Delete from stack");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the data");
scanf("%d",&num);
push(num);
display();
break;
case 2:
a=pop();
printf("Value returned from top of the stack is %d",a);
break;
default:
printf("Invalid choice");
}
printf("Do you want to continue, press 1");
scanf("%d",&wish);
}
}
void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node));
printf(" Address of newly created node x is %d",x);
x->data = y;
x->link = top;
top = x;
}
void display()
{
int i =0;
struct node * temp;
temp = top;
while(temp!=NULL)
{
printf("Item No. %d : Data %d Link %d ",i++,temp->data,temp->link);
temp=temp->link;
}
}
/// THIS FUNCTION REMOVES TOP NODE FROM THE STACK AND
RETURNS ITS VALUE///
int pop()
{
int a;
if(top==NULL)
{
printf("STACK EMPTY...");
return 0;
}
else
{
a=top->data;
printf("The value returned is %d ",a);
free(top);
top=top->link;
return (a);
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct A
{
int data;
struct node *link;
} ;
struct node *front, *rear;
void main()
{
int wish,will,a,num;
void add(int);
wish=1;
clrscr();
front=rear=NULL;
printf("Program for Queue as Linked List demo..");
while(wish == 1)
{
printf("Main Menu 1.Enter data in queue 2.Delete from queue");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the data");
scanf("%d",&num);
add(num);
//display();
break;
case 2:
a=del();
printf("Value returned from front of the queue is %d",a);
break;
default:
printf("Invalid choice");
}
printf("Do you want to continue, press 1");
scanf("%d",&wish);
}
getch();
}
void add(int y)
{
struct node *ptr;
ptr=malloc(sizeof(struct node));
ptr->data=y;
ptr->link=NULL;
if(front ==NULL)
{
front = rear= ptr;
}
else
{
rear->link=ptr;
rear=ptr;
}
}
int del()
{
int num;
if(front==NULL)
{
printf("QUEUE EMPTY");
return(0);
}
else
{
num=front->data;
front = front->link;
printf(" Value returned by delete function is %d ",num);
return(num);
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
struct barbie
{
int coff;
int pow;
struct barbie *link;
}*ptr,*start1,*A,*start2,*start3,*ptr1,*ptr2;
typedef struct barbie bar;
int temp1,temp2;
void main()
{
void create(void);
void prnt(void);
void suml(void);
void sort(void);
clrscr();
getch();
/*-----------------------------------------------------------------------------*/
void create()
{
char ch;
while(1)
{
printf(" Enter the coff and pow :");
scanf("%d%d",&node->coff,&node->pow);
if (node->pow==0 )
{
ptr=node;
node=(bar *)malloc(sizeof(bar));
node=NULL;
ptr->link=node;
break;
}
while(ptr!=NULL )
{
if(i!=1)
printf("+ ");
printf(" %dx %d ",ptr->coff,ptr->pow);
ptr=ptr->link;
i++;
}
//printf(" %d %d",ptr->coff,ptr->pow);
}
/*---------------------------------------------------------------------------*/
void sort()
{
for(;ptr->coff!=NULL;ptr=ptr->link)
for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)
{
if(ptr->pow>ptr2->pow)
{
temp1=ptr->coff;
temp2=ptr->pow;
ptr->coff=ptr2->coff;
ptr->pow=ptr2->pow;
ptr2->coff=temp1;
ptr2->pow=temp2;
}
}
}
/*---------------------------------------------------------------------------*/
void suml()
{
node=(bar *)malloc (sizeof(bar));
start3=node;
ptr1=start1;
ptr2=start2;
OUTPUT
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
struct dlinklist
{
struct dlinklist *prev; /** Stores address of previous node **/
int roll_no; /** stores roll number **/
char name[N]; /** stores Name **/
float marks; /** stores Marks **/
struct dlinklist *next; /** stores address of next node **/
};
void main()
{
node *head;
char ch; /* Choice inputing varible */
int opt; /* Option inputing variable*/
static int flag=0; /* Unchanged after iniialization */
clrscr();
head=(node*)malloc(sizeof(node));
head->next=NULL;
head->prev=NULL;
do
{
again:
printf("\nEnter your option\n");
printf("\n1. Initialize the node\n");
printf("\n2. Insert before a specified node\n");
printf("\n3. Insert after a specified node\n");
printf("\n4. Delete a particular node\n");
printf("\n5. Search the nodes\n");
printf("\n6. Display all the nodes\n");
scanf("%d",&opt);
temp=current;
while(temp->next!=NULL)
{
/*** Insertion checking for all node except first ***/
if(temp->next->roll_no==rno)
{
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
return(current);
}
temp=temp->next;
}
/*
If the function does not return from any return statement.
There is no match to insert before the input roll number.
*/
printf("\nMatch not found\n");
return(current);
}
node* del(node *current)
{
int rno; /* Roll number for deleting a node*/
node *newnode,*temp;
printf("\nEnter the roll number whose node you want to delete\n");
scanf("%d",&rno);
newnode=current;
if(current->roll_no==rno)
{
/*** Checking condition for deletion of first node ***/
newnode=current; /* Unnecessary step */
current=current->next;
current->prev=NULL;
free(newnode);
return(current);
}
else
{
while(newnode->next->next!=NULL)
{
/*** Checking condition for deletion of ***/
/*** all nodes except first and last node ***/
if(current->next->roll_no==rno)
{
newnode=current;
temp=current->next;
newnode->next=newnode->next->next;
newnode->next->prev=current;
free(temp);
return(current);
}
newnode=newnode->next;
}
if(newnode->next->next==NULL && newnode->next->roll_no==rno)
{
/*** Checking condition for deletion of last node ***/
temp=newnode->next;
free(temp);
newnode->next=NULL;
return(current);
}
}
printf("\nMatch not found\n");
return(current);
}
OUTPUT
MENU
1. Initialize the node
2. Insert before a specified node
3. Insert after a specified node
4. Delete a particular node
5. Search the nodes
6. Display all the nodes
Now you can insert a node
Enter your option : 1
Enter Roll number
60,61,79,81.
Enter the name
ALISHA,VIBHA,SATENDRA,SUNNY
Enter the marks
92,93,93,95
Enter the roll number after which you want to insert a node
61
Enter Roll number
60,61,68,79,81.
Enter the name
ALISHA,VIBHA,SACHIN,SATENDRA,SUNNY
Enter the marks
92,93,90,93,95
Enter the roll number before which you want to insert a node
68
Enter Roll number
60,61,64,68,79,81.
Enter the name
ALISHA,VIBHA,DEEPIKA,SACHIN,SATENDRA,SUNNY
Enter the marks
92,93,92,90,93,95