Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
140 views

C Program For The Create Add Delete Elements Using Single Linked List

This document contains C program code snippets for implementing various data structures using pointers and linked lists. These include: 1) A single linked list program for create, add, delete elements 2) Sorting a linked list in ascending order 3) Converting a prefix expression to postfix using stacks 4) Reversing a string using stacks 5) Implementing multiple stacks in a single array 6) Implementing a queue using pointers 7) Reversing elements in a queue

Uploaded by

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

C Program For The Create Add Delete Elements Using Single Linked List

This document contains C program code snippets for implementing various data structures using pointers and linked lists. These include: 1) A single linked list program for create, add, delete elements 2) Sorting a linked list in ascending order 3) Converting a prefix expression to postfix using stacks 4) Reversing a string using stacks 5) Implementing multiple stacks in a single array 6) Implementing a queue using pointers 7) Reversing elements in a queue

Uploaded by

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

//C PROGRAM FOR THE CREATE

ADD DELETE ELEMENTS USING


SINGLE LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct info
{
char name[30];
int eno;
struct info *next;
};
struct info
*head=NULL,*temp,*disp;
void addrecord();
void deleterecord();
void disrecord();
void main()
{
int ch;
clrscr();
while (1)
{
printf("\n 1. To add records\n");
printf("\n 2. To delete a
records\n");
printf("\n 3. To view the
records\n");
printf("\n 4. To exit\n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:addrecord();
break;
case 2:deleterecord();
break;
case 3: disrecord();
break;
case 4:exit(0);
} } }
void addrecord()
{
struct info *add;
char ans='y';
while (ans=='y')
{
add=(struct
info*)malloc(sizeof(struct info));
printf("\n Enter the names:\n");
gets(add->name);
fflush(stdin);
printf("\n Enter the enrollment
number:\n");
scanf("%d",&add->eno);
fflush(stdin);
if (head==NULL)
{
head=add;
add->next=NULL;
temp=add;
}
else
{
temp->next=add;
add->next=NULL;
temp=add;
}
printf("\n Would you like to enter
another name(y\\n): \n");
ans = getchar();
fflush(stdin);
}
} void deleterecord()
{
struct info *delete;
int teno, present=0;
if (head==NULL)
{
printf("\n No records to
delete\n");
return;
}
printf("\n Enter the enrollment
number to be deleted \n");
scanf("%d",&teno);
fflush(stdin);
for
(delete=head;delete!=NULL;delete
=delete->next)
{
if (delete->eno==teno)
{
if (head->eno==teno)
{
delete=head;
head=head->next;
free(delete);
return;
}
else
{
temp->next=delete->next;
free(delete);
return;
} }
temp=delete;
}
if (present==0)
printf("\nNo such enrollment
number present\n");
}
void disrecord()
{
if (head==NULL)
{
printf("\n No records to view\n");
return;
}
for
(disp=head;disp!=NULL;disp=disp-
>next)
{
printf("\n\n Name : %s",disp-
>name);
printf("\n\n Number : %d",disp-
>eno);
} }


//C PROGRAM TO SORT LINKED
LIST IN ASCENDING ORDER


#include<stdio.h>
#include<stdlib.h>
struct info
{
char name[30];
int eno;
struct info *next;
};
struct info *temp,*disp,*head;
void addrecord();
void disrecord();
void main()
{
int ch;
clrscr();
while (1)
{
printf("\n 1. To add records\n");
printf("\n 2. To view the
records\n");
printf("\n 3. To exit\n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:addrecord();
break;
case 2:disrecord();
break;
case 3: exit(0);
}
}
}
void addrecord()
{
struct info *add;
char ans='y';
while (ans=='y')
{
add=(struct
info*)malloc(sizeof(struct info));
printf("\n Enter the name:\n");
gets(add->name);
fflush(stdin);
printf("\n Enter the enrollment
number:\n");
scanf("%d",&add->eno);
fflush(stdin);
if (head==NULL|| head-
>eno>=add->eno)
{
add->next=head;
head=add;
}
else
{
temp=head;
while (temp->next!=NULL &&
temp->next->eno < add->eno)
{
temp=temp->next;
}
add->next=temp->next;
temp->next=add;
}
printf("\n Would you like to enter
another name(y\\n): \n");
ans = getchar();
fflush(stdin);
}
}
void disrecord()
{
if (head==NULL)
{
printf("\n No records to view\n");
return;
}
for
(disp=head;disp!=NULL;disp=disp-
>next)
{
printf("\n\n Name : %s",disp-
>name);
printf("\n\n Number : %d",disp-
>eno);
}
}



//C PROGRAM TO CONVERT A
PREFIX EXPRESSION TO A POST
FIX USING POINTERS
#include<stdio.h>
#include<string.h>
void push(char item[],int
*top,char s[][20])
{
*top=*top+1;
strcpy(s[*top],item);
}
void *pop(int *top,char s[][20])
{
char *item;
item=s[*top];
*top=*top-1;
return item;
}
void pre_post(char prefix[],char
postfix[])
{
char s[20][20];
int top,i;
char symbol,temp[2];
char *op1,*op2;
top=-1;
strrev(prefix);
for(i=0;i<strlen(prefix);i++)
{
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
switch (symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:
push(temp,&top,s);
} } }
void main()
{
char prefix[20];
char postfix[20];
printf("\n\n Enter the prefix
expression \n\n");
scanf("%s",prefix);
pre_post(prefix,postfix);
printf("\n\n The postfix expression
is %s \n\n",postfix);
}

//C PROGRAM TO REVERSE AN
INPUT STRING USING STACKS

#include<stdio.h>
#include<string.h>
#define STACK_SIZE 20
void push(char item,int *top,char
s[])
{
if (*top==STACK_SIZE-1)
{
printf("\n stack overflow\n");
return;
}
s[++(*top)]=item;
}
char pop(int *top,char s[])
{
char item_deleted;
if (*top==-1)
{
return 0;
}
item_deleted=s[(*top)--];
return item_deleted;
}
int is_rev(char str[])
{
int i;
int top=-1;
char s[30] ;
char stk_item=0;
for(i=0;i<strlen(str);i++)
{
push (str[i],&top,s);
}
printf("\n The reversed string is:");
for(i=0;i<strlen(str);i++)
{
stk_item= pop (&top,s);
printf("%c",stk_item);
}
getch();
}
void main()
{
char str[20];
clrscr();
printf("\n Enter the string to be
reversed\n");
scanf("%s",str);
is_rev(str); }

//C PROGRAM TO IMPLEMENT
MULTIPLE STACK IN A SINGLE
ARRAY
or
WRITE A C PROGRAM TO
IMPLEMENT MORE THAN ONE
STACK IN SINGLE ARRAY


#include <STDIO.H>
#define MAX 10
int stack[MAX],topA=-
1,topB=MAX;
void pushA(int no)
{
if(topA==topB)
{
printf("\n OVERFLOW");
return;
}
stack[++(topA)]=no;
}
int popA()
{
if(topA==-1)
{
printf("\n UNDERFLOW");
return -999;
}
return stack[(topA)--];
}
void showA()
{
int i;
if(topA==-1)
{
printf("\n stack Empty");
return;
}
for(i=topA;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
void pushB(int no)
{
if(topB-1==topA)
{
printf("\n OVERFLOW");
return;
}
stack[--(topB)]=no;
}
int popB()
{
if(topB==MAX)
{
printf("\n UNDERFLOW");
return -999;
}
return stack[(topB)--];
}
void showB()
{
int i;
if(topB==MAX)
{
printf("\n stack Empty");
return;
}
for(i=topB;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}


//C PROGRAM TO IMPLEMENT
QUEUE USING POINTERS


#include < stdio.h>
#include < conio.h>
#include < malloc.h>
#include < process.h>
#include < ctype.h>
struct linear_queue
{
int info;
struct linear_queue *next;
}*front,*rear,*newnode,*ptr;
void menu();
void display();
int underflow();
void enqueue(int);
void dequeue();
void main()
{
clrscr();
menu();
}
void menu()
{
int choice,item;
printf("MENU");
printf("\n1. Insert into the
queue");
printf("\n2. Delete from queue");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
printf("\nEnter the item tobe
inserted: ");
scanf("%d",&item);
enqueue(item);
clrscr();
printf("\nAfter inserting queue
is:\n");
display();
getch();
clrscr();
menu();
break;
case 2:
clrscr();
if(underflow()==1)
{
dequeue();
if(underflow()==1)
{
printf("\nAfter deletion queue
is:\n");
display();
}
}
getch();
clrscr();
menu();
break;
case 3:
clrscr();
if(underflow()==1)
{
printf("The queue is:\n");
display();
}
getch();
clrscr();
menu();
break;
case 4:
exit(1);
default:
clrscr();
printf("Your choice is wrong\n\n");
menu();
}
} int underflow()
{
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is empty");
return(0);
}
else
{
return(1);
}
} void enqueue(int item)
{
newnode=(struct
linear_queue*)malloc(sizeof(struct
linear_queue));
newnode->info=item;
if((front==NULL)&&(rear==NULL))
{
front=newnode;
rear=newnode;
newnode->next=NULL;
}
else
{
rear->next=newnode;
newnode->next=NULL;
rear=newnode;
}
} void dequeue()
{
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
}
}
void display()
{
int i;
ptr=front;
i=1;
while(ptr!=NULL)
{
printf("\nNode %d : %d",i,ptr-
>info);
ptr=ptr->next;
i++;
}
}






//C PROGRAM TO IMPLEMENT
QUEUE USING POINTERS


#include < stdio.h>
#include < conio.h>
#include < malloc.h>
#include < process.h>
#include < ctype.h>
struct linear_queue
{
int info;
struct linear_queue *next;
}*front,*rear,*newnode,*ptr;
void menu();
void display();
int underflow();
void enqueue(int);
void dequeue();
void main()
{
clrscr();
menu();
}
void menu()
{
int choice,item;
printf("MENU");
printf("\n1. Insert into the
queue");
printf("\n2. Delete from queue");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
printf("\nEnter the item tobe
inserted: ");
scanf("%d",&item);
enqueue(item);
clrscr();
printf("\nAfter inserting queue
is:\n");
display();
getch();
clrscr();
menu();
break;
case 2:
clrscr();
if(underflow()==1)
{
dequeue();
if(underflow()==1)
{
printf("\nAfter deletion queue
is:\n");
display();
}
}
getch();
clrscr();
menu();
break;
case 3:
clrscr();
if(underflow()==1)
{
printf("The queue is:\n");
display();
}
getch();
clrscr();
menu();
break;
case 4:
exit(1);
default:
clrscr();
printf("Your choice is wrong\n\n");
menu();
}
} int underflow()
{
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is empty");
return(0);
}
else
{
return(1);
}
} void enqueue(int item)
{
newnode=(struct
linear_queue*)malloc(sizeof(struct
linear_queue));
newnode->info=item;
if((front==NULL)&&(rear==NULL))
{
front=newnode;
rear=newnode;
newnode->next=NULL;
}
else
{
rear->next=newnode;
newnode->next=NULL;
rear=newnode;
}
} void dequeue()
{
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
}
}
void display()
{
int i;
ptr=front;
i=1;
while(ptr!=NULL)
{
printf("\nNode %d : %d",i,ptr-
>info);
ptr=ptr->next;
i++;
}
}


//C PROGRAM TO REVERSE
ELEMENTS IN A QUEUE


#include<stdio.h>
#include<stdlib.h>
struct stackrecord
{
int *array;
int capacity;
int tos;
};
typedef struct stackrecord *stack;
stack createstack(int max)
{
stack s;
s=malloc(sizeof(struct
stackrecord));
if(s==NULL)
{
printf("out of space");
}
s-
>array=malloc((sizeof(int))*max);
if(s->array==NULL)
{
printf("out of space");
}
s->capacity=max-1;
s->tos=-1;
return(s);
}
int isemptys(stack s)
{
return s->tos==-1;
}
int isfulls(stack s)
{
return s->tos==s->capacity;
}
void push(int x,stack s)
{
if(isfulls(s))
printf("Overflow");
else
{
printf("\n %d is pushed",x);
s->tos++;
s->array[s->tos]=x;
}
}
int topandpop(stack s)
{
if(isemptys(s))
{
printf("\n empty stack");
return;
}
else
{
printf("\n %d is popped",s-
>array[s->tos]);
return s->array[s->tos--];
}
}
struct queuerecord
{
int *array;
int front;
int rear;
int capacity;
};
typedef struct queuerecord
*queue;
queue createqueue(int max)
{
queue q;
q=malloc(sizeof(struct
queuerecord));
if(q==NULL)
printf("Error");
q->array=malloc(sizeof(int)*max);
if(q->array==NULL)
printf("Error");
q->capacity=max-1;
q->front=-1;
q->rear=-1;
return q;
}
int isfullq(queue q)
{
return (q->rear==q->capacity);
}
int isemptyq(queue q)
{
return (q->front==-1);
}
void enqueue(queue q,int x)
{
if(isfullq(q))
printf("overflows");
else
{
printf("\n %d is enqueued",x);
q->rear++;
q->array[q->rear]=x;
if(q->front==-1)
q->front++;
}
}
int frontanddelete(queue q)
{
int p;
if(isemptyq(q))
{
printf("underflow");
return;
}
else
{
p=q->array[q->front];
printf("\n %d is front and
deleted",p);
q->front++;
return p;
}
}
void display(queue q)
{
int i;
if(isemptyq(q))
{
printf("underflow");
return;
}
for(i=q->front;irear;i++)
printf("%d\t",q->array[i]);
}
int main()
{
int max,ele,i,choice,n=0,y,z;
queue q;
stack s;
printf("\n Enter the maximum
elements:");
scanf("%d",&max);
q=createqueue(max);
s=createstack(max);
while(1)
{
printf("\n Menu:1.Insert 2.Display
reversed order 3.exit");
printf("\n Enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the element:");
scanf("%d",&ele);
enqueue(q,ele);
n++;
break;
case 2:
printf("\n Contents of the
queue:");
display(q);
for(i=0;i< capacity;i++)
{
z=frontanddelete(q),s;
push(z,s);
}
q->front=-1;
q->rear=-1;
for(i=0;i< capacity;i++)
{
y=topandpop(s);
enqueue(q,y);
}
printf("\n Reversed contents
are:");
display(q);
break;
case 3:
exit(0);
}
}
}

You might also like