Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.

Similar presentations


Presentation on theme: "UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India."— Presentation transcript:

1 UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India

2 UNIT 1 LINKED LIST  A Linked List is a linear collection of data elements called nodes, where the linear order is given by means of pointers.  The each node is divided into two parts: the first part contain the information of the element, and the second part, called the link field contains the address of t he next node in the list. x START LINK LIST WITH 4 NODES

3 UNIT 1  The pointer of the last node contains a special value called a null pointer which is any invalid address.  The value of null character is either ’0’ or ‘any negative value’.  The linked list also contains a list pointer variable called start which contains the address of the first node of the link list.  A special case in a list is that has no node such that list is called a null or empty list and it is represented by the null pointer in the start.  Example:- create a link list for marks of five subject 89,75,78,80,74 in ascending order

4 UNIT 1 89 75 78 80 74 5 START 2 3 4 1 0 LINK LIST REPRESENTATION OF THE MARKS

5 UNIT 1 ALGORITHM FOR TRAVERSING A LINKED LIST 1.Set PTR=START [Initializes pointer PTR] 2.Repeat steps 3 & 4 while PTR != NULL 3.Apply process to INFO[PTR] 4. Set PTR:- LINK[PTR] [PTR now points to next node] [End of step 2 loop] 5. Exit

6 UNIT 1 PTR PTR=LINK[PTR]

7 UNIT 1 ALGORITHM FOR SEARCHING IN A UNSORTED LINKED LIST SEARCH(INFO,LINK,START,ITEM,LOC) 1.SET PTR = START 2.REPEAT STEP 3 WHILE PTR != NULL 3.IF ITEM = INFO[PTR] then SET LOC = PTR & EXIT ELSE SET PTR= LINK[PTR] [PTR now points to the next node][END OF IF STRUCTURE] [END OF STEP 2 LOOP] 4.[SEARCH IS UNSUCESSFUL] SET LOC=NULL 5.EXIT

8 UNIT 1 ALGORITHM FOR SEARCHING IN SORTED LIST SRCHSL(INFO,LINK,START,ITEM,LOC) 1.SET PTR = START 2.REPEAT STEP 3 WHILE PTR != NULL 3.IF ITEM < INFO[PTR] then; SET PTR = LINK[PTR] [PTR now points to the next node] ELSE IF ITEM = INFO [PTR] then; SET LOC = PTR & EXIT [search is successful] ELSE SET LOC = NULL & EXIT [item now exceed INFO[PTR]] [END OF IF STRUCTURE] [END OF STEP 2 LOOP] 4.[SEARCH IS UNSUCESSFUL] SET LOC=NULL 5.EXIT

9 UNIT 1 OVERFLOW & UNDERFLOW CONDITIONS OVERFLOW will occur with our link list when AVAIL= NULL &there is an insertion UNDERFLOW will occur with our link list when START=NULL & there is a deletion.

10 UNIT 1 INSERTION IN A LINKED LIST This Can Be Done In Three Ways: 1.Beginning of the list 2.Between two nodes 3.In a sorted list

11 UNIT 1 AT BEGINNING OF THE LINKED LIST x START x AVAIL

12 UNIT 1 x START x AVAIL

13 UNIT 1 ALGORITHM FOR INSERTION AT BEGINNING INSFIRST(INFO,LINK,START,AVAIL,ITEM) 1.[OVERFLOW] if AVAIL=NULL, then write OVERFLOW & EXIT 2.[ Remove first node from AVAIL list] set NEW = AVAIL & AVAIL= LINK[AVAIL] 3.Set INFO[NEW]=ITEM [copies new data into new node] 4.Set LINK[NEW]= START [new node points to original first node] 5.Set START= NEW [changes START so it points to the new node] 6.exit

14 UNIT 1 INSERT AFTER A GIVEN NODE x START x AVAIL Node ANode B

15 UNIT 1 x START x AVAIL Node A Node B Node N

16 UNIT 1 ALGORITHM FOR INSERTION AT POSITION INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM) 1.[OVERFLOW] if AVAIL=NULL, then write OVERFLOW & EXIT 2.[ Remove first node from AVAIL list] set NEW = AVAIL & AVAIL= LINK[AVAIL] 3.Set INFO[NEW]=ITEM [copies new data into new node] 4.If LOC=NULL then [INSERT AS A FIRST NODE] set LINK[NEW]= START & START=NEW else [INSERT node after node with location LOC] set LINK[NEW]=LINK[LOC] & LINK[LOC]=NEW; [END OF IF STRUCTURE] 5.exit

17 UNIT 1 CREATION OF LINKED LIST #include void main() { struct node { int num; struct node *ptr; }; typedef struct node NODE; NODE *head, *first, *temp; int count=0; int choice=1; first=NULL; while(choice) { head=(NODE *)malloc(sizeof(NODE)); printf("Enter the data item\n"); scanf("%d",&head->num); if(first!=NULL) { temp->ptr=head; temp=head; }

18 UNIT 1 else { first=temp=head; } fflush(stdin); printf("Do you want to continue(type 0 or 1)?\n"); scanf("%d",&choice); } temp->ptr=NULL; temp=first; printf("Status of the linked list is\n"); while(temp!=NULL) { printf("%d",temp->num); count++; temp=temp->ptr; } printf("NULL"); printf("NO of nodes in the list =%d\n",count); getch(); }

19 UNIT 1 Output of the Program Enter the data item 1 Do you want to continue(type 0 or 1)? 1 Enter the data item 2 Do you want to continue(type 0 or 1)? 1 Enter the data item 3 Do you want to continue(type 0 or 1)? 1 Enter the data item 4 Do you want to continue(type 0 or 1)? 0 Status of the linked list is 1234NULLNO of nodes in the list =4

20 UNIT 1 Insertion in Linked List #include struct node { int info; struct node *next; }; typedef struct node NODE; NODE *start; void createmptylist(NODE *start) { *start=(NODE *)NULL; } void traversinorder(NODE *start) { while(start != (NODE *) NULL) { printf("%d\n",start->info); start=start->next; }

21 UNIT 1 void insertatbegin(int item) { NODE *ptr; ptr=(NODE *)malloc(sizeof(NODE)); ptr->info=item; if(start==(NODE *)NULL) ptr->next=(NODE *)NULL; else ptr->next=start; start=ptr; } void insert_at_end(int item) { NODE *ptr,*loc; ptr=(NODE *)malloc(sizeof(NODE)); ptr->info=item; ptr->next=(NODE *)NULL; if(start==(NODE*)NULL) start=ptr;

22 UNIT 1 else { loc=start; while(loc->next!=(NODE *)NULL) loc=loc->next; loc->next=ptr; } void insert_spe(NODE *start,int item) { NODE *ptr,*loc; int temp,k; for(k=0,loc=start;k<temp;k++) { loc=loc->next; if(loc==NULL) { printf("node in the list at less than one\n"); return; }

23 UNIT 1 ptr=(NODE *)malloc(sizeof(NODE)); ptr->info=item; ptr->next=loc->next;; loc->next=ptr; } void main() { int choice,item,after; char ch; clrscr(); createmptylist(start); do { printf("1.Insert element at begin \n"); printf("2. insert element at end positon\n"); printf("3. insert specific the position\n"); printf("4.travers the list in order\n"); printf("5. exit\n");

24 UNIT 1 printf("enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the item\n"); scanf("%d",&item); insertatbegin(item); break; case 2: printf("Enter the item\n"); scanf("%d",&item); insert_at_end(item); break; case 3: printf("Enter the item\n"); scanf("%d",&item); insert_spe(start,item); break;

25 UNIT 1 case 4: printf("\ntravers the list\n"); traversinorder(start); break; case 5: return; } fflush(stdin); printf("do your want continous(y for yes)\n"); scanf("%c",&ch); }while((ch='y')||(ch='Y')); getch(); }

26 UNIT 1 THANKS


Download ppt "UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India."

Similar presentations


Ads by Google