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
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
136 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
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