My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
PROGRAM FOR LINK LIST IN C++
Posted Date: 16 Feb 2008 Resource Type: Articles/Knowledge Sharing Category: Computer & Technology
|
Posted By: Gurpreet Singh Member Level: Gold Rating: Points: 3
|
|
|
|
//PROGRAM FOR LINKED LIST..............//
#include #include #include
struct node { int info; struct node *link; };
typedef struct node NODE;
NODE *start,*p,*ptr;
NODE * create_node() //function for creating a node { p=(NODE *)malloc(sizeof (NODE)); printf("\nENTER THE INFORMATION : "); scanf("%d",&p->info); p->link=NULL; return p; }
void traverse() //function for traversing the list { ptr=start; while(ptr!=NULL) { printf("\n%d",ptr->info); ptr=ptr->link; } }
void insert_at_beg() //function for insertion at beggining { ptr=create_node(); if(start==NULL) ptr->link=NULL; else ptr->link=start; start=ptr; }
void insert_at_mid() //function for insertion in middle { int pos,i; NODE *temp; ptr=create_node(); printf("\nENTER THE POSITIONB AT WHICH NODE IS TO BE INSERTED : "); scanf("%d",&pos); temp=start; for(i=1;i temp=temp->link; ptr->link=temp->link; temp->link=ptr; }
void insert_at_end() //function for insertion at end { NODE *temp; ptr=create_node(); temp=start; while(temp->link!=NULL) temp=temp->link; temp->link=ptr; }
void deletion_at_beg() //function for deletion at beggining { int item; ptr=start; start=ptr->link; item=ptr->info; printf("\nITEM DELETED IS : %d",item); free (ptr); }
void deletion_at_mid() //function for deletion in middle { int item,pos,i; NODE *temp,*temp1; printf("\nENTER THE POSITION FROM WHICH NODE IS TO BE DELETED : "); scanf("%d",&pos); ptr=start; for(i=1;i { temp=ptr; ptr=ptr->link; } item=ptr->info; printf("\nITEM DELETED IS : %d",item); temp->link=ptr->link; free (ptr); }
void deletion_at_end() //function for deletion at end { int item; NODE *temp; ptr=start; while(ptr->link!=NULL) { temp=ptr; ptr=ptr->link; } temp->link=NULL; item=ptr->info; printf("\nITEM DELETED IS : %d",item); free (ptr); }
void main() { int ch,ans,choice; clrscr(); printf("\nCREATING THE LINK LIST.......\n"); start=create_node(); ptr=start; for( ; ;) { printf("\nDO YOU WANT TO ENTER MORE NODE?(Y/N) : "); scanf("%s",&ans); if(ans=='y' || ans=='Y') { ptr->link=create_node(); ptr=ptr->link; } else break; } printf("\n\nMENU:-\n"); printf("\n1.TRAVERSING THE LIST\n"); printf("\n2.INSERTION AT BEGGINING\n"); printf("\n3.INSERTION AT MIDDLE\n"); printf("\n4.INSERTION AT END\n"); printf("\n5.DELETION AT FRONT\n"); printf("\n6.DELETION AT MIDDLE\n"); printf("\n7.DELETION AT END\n"); do { printf("\n\nENTER YOUR CHOICE(1-7) : "); scanf("%d",&ch); switch(ch) { case 1: traverse(); break; case 2: insert_at_beg(); break; case 3: insert_at_mid(); break; case 4: insert_at_end(); break; case 5: deletion_at_beg(); break; case 6: deletion_at_mid(); break; case 7: deletion_at_end(); break; default: printf("\n\nWRONG CHOICE ENTERED!!!!"); } printf("\n\nDO YOU WANT TO CONTINUE?(Y/N) : "); scanf("%s",&choice); }while(choice=='y' || choice=='Y'); getch(); }
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
|