| Forums.Sureshkumar.net : A Perfect Place to Share Knowledge Blogs Games Magazines |
|
|
#1 (permalink) |
|
Junior Member
Join Date: Jul 2006
Posts: 10
Thanks: 0 Thanked 0 Times in 0 Posts Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
|
Linked list ..help
Plz anyone give me code for
1.singly lkedlist 2.doubly linked list 3.circular lnkedlist 4.reverse linked list using structure in C at leat explain hw to delete item from middle,add item to middle,serching for item etc rahul dev datta. |
|
|
|
|
|
#2 (permalink) |
|
Member
Join Date: Aug 2006
Age: 23
Posts: 34
Thanks: 1 Thanked 0 Times in 0 Posts Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 3
|
Re: Linked list ..help
//Program for linked list
#include #define TRUE 1 struct node{ int info; struct node *next; }; typedef struct node *nodeptr; nodeptr p,tp; void startlist() { int x; p= getnode(); tp = p; printf("Enter an item to insert:"); scanf("%d",&x); p->info = x; p->next = NULL; } void showlist() { p = tp; printf("node(p)\t Info\t Next"); while(p!= NULL) { printf("\n %d \t%d \t%d ",p,p->info,p->next); p=p->next; } putchar('\n'); } void insafter(noteptr p,int x) { nodeptr a; if(p==NULL) { printf("void insertion"); exit(1); } q= getnode(); q->info = x; q->next = p->next; p->next = q; } nodeptr getnode() { nodeptr p; p =(nodeptr) malloc(sizeof(struct node)); return(p); } void freenode(nodeptr p) { free(p); } void deleteafter(nodeptr p) { nodeptr q; if(p==NULL ::q->next ==NULL) { printf("void deletion"); exit(1); } q=q->next; p->next = q->next; freenode(q); } void main() { int choice,i,x; nodeptr n; nodeptr getnode(); clrscr(); printf("\n Enter Ur choice 1-> start 2->showlist 3->insert 4->delete 5->end \n"); while (TRUE) { printf("Enter ur choice"); scanf("%d",&choice); switch(choice) { case 1: startlist(); break; case 2: showlist(); break; case 3: printf ("Node Insertion: after which node?:"); scanf("%d",&n); p=n; printf("Enter the item for insertion:"); scanf("%d",&x); insafter(p,x); break; case 4: printf("Node deletion:After which node?:"); scanf("%d",&n); p=n; delafter(p);break; case 5: exit(5); } } } |
|
|
|
|
|
#3 (permalink) |
|
Member
Join Date: Aug 2006
Age: 23
Posts: 34
Thanks: 1 Thanked 0 Times in 0 Posts Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 3
|
Re: Linked list ..help
Program to illustrate queue using linked list concept:
/*Program to illustrate queue using linked list concept*/ #include struct node /*Structure used to represent elements as nodes*/ { int val; struct node *next; }*rear = NULL,*front = NULL,*temp = NULL; void enqueue(int); /*Function declaration section*/ int dequeue(); void disp(); /*Main Function starts here*/ void main() { int opt,item,dele; /* do { clrscr(); printf("Enter the required option:\n1.Enqueue\n2.Dequeue\n3.View the queue\n4.Exit:"); scanf("%d",&opt); /*Required option is received here*/ switch(opt) { case 1: printf("Enter the value to enqueue:"); scanf("%d",&item); enqueue(item); /*Element added to the queue */ break; case 2: /*Element removed from the queue pointed by the front pointer*/ dele = dequeue(); if(dele == 0) { disp(); continue; } getch(); break; case 3: disp(); /*Displays the queue when called upon*/ break; } }while(opt != 4); /*Checks for the entered option*/ } void enqueue(int item) { if(rear == NULL) { /*Reserving memory space using malloc*/ rear = (struct node *)malloc(sizeof(struct node)); /*Assigning the first value to the rear pointer*/ rear->val = item; /*Assigning the next of rear pointer to NULL*/ rear->next = NULL; /*Assigning the front pointer to the rear pointer*/ front = rear; } else { /*Reserving memory space using malloc*/ rear->next = (struct node *)malloc(sizeof(struct node)); /*Assigning the value of the next element of rear pointer*/ rear->next->val = item; /*Assigning the next of next element of rear pointer to NULL*/ rear->next->next = NULL; /*Moving rear by one node*/ rear = rear->next; } } /*Function used to dequeue an element*/ int dequeue() { int dequeued; /*Variable used to store the dequeued element*/ if(front == NULL) /*Checking for the queue to be NULL*/ { printf("\nQueue is empty\n"); /*Printing the queue is empty*/ return; } dequeued = front->val; /*Assigning the dequeued value to a variable*/ /*Printing the dequeued element*/ printf("The dequeued item is %d",front->val); temp = front; /*Temp initialized to front*/ front = front->next; /*Element is deleted by moving the front pointer*/ free(temp); /*Temp is made free*/ if(front == NULL) /*Checking for the queue to be NULL*/ rear = NULL; return dequeued; /*Dequeued value is returned here*/ } /*Function to display the queue elements*/ void disp() { if(front == NULL) /*Checking for the queue to be NULL*/ { printf("The queue is empty"); getch(); return; } temp = front; /*Temp initialized to front pointer*/ printf("\nQueue elements are:\n"); /* while(temp != NULL) { printf("%d\n",temp->val); temp = temp->next; /*Temp is passed to the next node by pointers*/ } getch(); } Output: Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 The queue is empty Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:15 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:69 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 Queue elements are: 15 69 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:39 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:25 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:1 Enter the value to enqueue:30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 Queue elements are: 15 69 39 25 30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 15 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 69 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:3 Queue elements are: 39 25 30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 39 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 25 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 The dequeued item is 30 Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:2 Queue is empty Enter the required option: 1.Enqueue 2.Dequeue 3.View the queue 4.Exit:4 ------- |
|
|
|
|
|
#4 (permalink) |
|
Member
Join Date: Aug 2006
Age: 23
Posts: 34
Thanks: 1 Thanked 0 Times in 0 Posts Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 3
|
Re: Linked list ..help
Program to illustrate stack using Linked list concept:
/*Program to illustrate stack using linked list concept*/ #include struct node /*Structure used to represent elements as nodes*/ { int val; /*Element*/ struct node *next; /*Next pointer*/ }*top = NULL,*temp = NULL; void push(int); /*Function declaration section*/ int pop(); void disp(); /*Main function starts here*/ void main() { int opt,item,popped; /* do { clrscr(); printf("Enter the required option\n1.Push\n2.Pop\n3.View the stack\n4.exit:"); scanf("%d",&opt); /*Option is received here*/ switch(opt) { case 1: printf("Enter the element to push:"); scanf("%d",&item); push(item); /*Element pushed into the stack*/ break; case 2: popped = pop(); /*Element popped out of the stack*/ if(popped == 0) { disp(); continue; } printf("Popped element = %d",popped); getch(); break; case 3: disp(); /*Used to diplay the stack*/ break; } }while(opt != 4); } /*Function to push an element into the stack*/ void push(int item) { if(top == NULL) /*Condition which checks whether first element*/ { /*Reserving memory space using malloc*/ top = (struct node *)malloc(sizeof(struct node)); /*Assigning the first element as the entered value*/ top->val = item; /*Assigning the next pointer to NULL*/ top->next = NULL; } else { /*Reserving memory space using malloc*/ temp = (struct node *)malloc(sizeof(struct node)); /*Asssigning the entered value into a temporary space*/ temp->val = item; /*Mapping the next pointer to the previous element*/ temp->next = top; /*Mapping the top of stack with the current element*/ top = temp; } } int pop() { int res; if(top == NULL) /*Checking for the stack to be empty*/ return(0); else { int pop; pop = top->val; /*Storing the popped value into a variable*/ temp = top; /*Manipulating the pointers to remove the element*/ top = top->next; free(temp); /*Making the temporary space free*/ return pop; /*Returning the value stored to pop out*/ } } void disp() { if(top == NULL) /*Condition checking for the stack to be empty*/ { printf("The stack is empty"); getch(); return; } temp = top; printf("The stack elements are:\n"); while(temp != NULL) /*Printing the stack elements*/ { printf("\n%d",temp->val); /*Moving the temporary pointer to the next element*/ temp = temp->next; } getch(); } Output: Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:3 The stack is empty Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:185 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:894 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:1 Enter the element to push:474 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit3 The stack elements are: 474 894 185 156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 474 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 894 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:3 The stack elements are: 185 156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 185 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 Popped element = 156 Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:2 The stack is empty Enter the required option 1.Push 2.Pop 3.View the stack 4.exit:4 |
|
|
|
|
|
#5 (permalink) |
|
Moderator
Join Date: Apr 2006
Location: India
Posts: 1,733
Thanks: 2 Thanked 145 Times in 134 Posts Thanks: 2
Thanked 145 Times in 134 Posts
Rep Power: 31
|
Re: Linked list ..help
hi varun,
instead of chkng code given by others its good 2 try 2 develop ur own code understanding the logic behind the qsn .... yashwant karnetkar - pointers in c book gives basic level explanations of all these concepts .... try 2 develop ur logic ....
__________________
Sree |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| LIST OF TOP 60+ INSTITUTES IN INDIA | wizkid | Other Queries | 1 | 06-08-06 01:01 AM |
| phobias | winner | Improve Your Language Skills | 4 | 29-06-06 08:45 PM |
| companies list | Tan | Companies Info & Recruitment Process | 7 | 17-03-06 05:11 AM |
| 10 new Indian Billionaires on Forbes list | keerthi | Latest Tech News & Innovations | 6 | 12-03-06 09:57 AM |
| JAVA collection | naga | JAVA Technologies | 0 | 21-02-06 06:01 AM |