You are on page 1of 15

Difference between Linked lists and arrays: An array is a static data structure.

. This means the length of array cannot be altered at run time. While, a linked list is a dynamic data structure. In an array, all the elements are kept at consecutive memory locations while in a linked list the elements (or nodes) may be kept at any location but still connected to each other.

Insertion Insert as a first node Insert as a last node Insert after a node Insert before a node Deletion Delete first node Delete last node Traversal

For insertion: A record is created holding the new item. The nextpointer of the new record is set to link it to the item which is to follow it in the list. The nextpointer of the item which is to precede it must be modified to point to the new item. For deletion: The nextpointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item.

head

93

48

17

142

Note: There is no work to find the correct location Empty or not, head will point to the right location

void insertf() { struct studinfo *newnode; newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next=NULL; if(start==NULL) start = newnode; else {

/*Creating new node*/

newnode->next = start; start = newnode;


} }

head

48

17

142

93 //

//

Note: Find the end of the list Recursion or iteration

void insertl() { struct studinfo *newnode, *ptr; newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next=NULL; if (start == NULL) start =newnode; else { ptr =start; while (ptr->next != NULL) ptr= ptr->next; ptr->next = newnode; } }

Head

17

48

142 93

142 //

//

Note: Used when order is important Go to the node that should follow the one to add, Recursion or iteration

void inserta() { int cnt=1, no; struct studinfo *ptr,*prevptr, *newnode; printf("\nEnter number ..."); scanf("%d",&no); ptr=start; while (cnt != no) { ptr = ptr->next; cnt++; } newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next =NULL; newnode->next = ptr->next; ptr->next = newnode; }

void insertb() { int cnt=1, no; struct studinfo *ptr,*prevptr, *newnode; printf("\nEnter number ..."); scanf("%d",&no); ptr=start; if(no==1) { insertf(); } else { while(cnt !=no) { prevptr=ptr; ptr = ptr->next; cnt++; }

newnode=(struct studinfo *) malloc(sizeof(struct studinfo)); printf("\nEnter a new record : marks and name "); scanf("%d%s",&newnode->marks,newnode->name); newnode->next=ptr; prevptr->next=newnode; } }

void deletef() { struct studinfo *ptr; if (start == NULL) { printf("\n List is empty"); return; } ptr=start; start=start->next; free(ptr); }

void deletel() { struct studinfo *ptr,*prevptr; ptr=start; if (ptr->next==NULL) start = NULL; else { while(ptr->next!=NULL) { prevptr=ptr; ptr=ptr->next; } prevptr->next =NULL; } free(ptr); }

void traverse() { struct studinfo *ptr; if (start == NULL) { printf("\n List is empty"); return; } ptr= start; while (ptr !=NULL) { printf("\nRecord: marks and name %d %s\n",ptr->marks, ptr->name); ptr = ptr->next; } getch(); }

You might also like