• Embed Doc
  • Readcast
  • Collections
  • 3
    CommentGo Back
Download
 
LIST IMPLEMENTATION USING LINKED LIST
#include<stdio.h>#include<stlib.h>#include<conio.h>struct node;typedef struct node *ptr;typedef ptr list;typedef ptr position;typedef int data;struct node{data element;struct node *next;}//function prototypesvoid makeempty(void);//to make empty listint isempty(void);//to check list is empty or notvoid create(void);//to create initial set of elements position findprevious(data);//to find position of previous elementvoid delet(data);//to delete given elementvoid display(void);//to display all the elementsvoid insert(data, int);//to insert a new element position getprevposition(int);//to find position of previous elementdata getelement(int);//to find the element at given positionint getposition(data);//to find position of given element//global variable declarations position first; position last; position L;int length;//to make empty listvoid makeempty(void){ position tmp;tmp = malloc(sizeof(list));tmp->next = NULL;L = tmp;first = last = NULL;}//to check list is empty or not
 
int isempty(void){if (L->next = NULL)return 1;elsereturn 0;}//to create initial set of elementsvoid create(void){data e;int n, i; position tmp;makeempty(); printf(“Enter number of element : \“);scanf(“%d”, &n);for (i=0; i<n; i++){ printf(“Enter an element : “);scanf(“%d”, &e);tmp = malloc(sizeof(list));tmp->element = e;tmp->next = NULL;if (L->next == NULL){L->next = tmp;first = last = tmp;}else{last->next = tmp;last = tmp;}}}//to display all the elementsvoid display(){ position t;for(t=first; t!=NULL; t=t->next) printf(“%d --> “, t->element);getch();}//to find position of previous element position getprevposition(int index){ position tmp;int count = 1;
 
if (index>length){ printf(“Invalid Position”);return NULL;}else{for (tmp=first; count<index-1; tmp=tmp->next)count++;return tmp;}}//to insert a new elementvoid insert(data x, int p){ position pos, tmp;tmp = malloc(sizeof(list));tmp->element=x;if (p==1)//first position{tmp->next = first;L->next = tmp;first = tmp;length++;}else if (p == length)//last position{last->next = tmp;last = tmp;tmp->next = NULL;}else//arbitrary position{ pos = getpreviousposition(p);if (pos == NULL){ printf(“Invalid position”);getch();}else{tmp->next = pos->next; pos->next = tmp;length++;}}}//to find position of previous element
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...

thanks it helps me to inmplement the link list program

You must be to leave a comment.
Submit
Characters: ...