singly linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
char data[5];
struct node *next;
}*start=NULL,**now=&start;
void Addnode();
void Shownode();
void Delnode();
void Insnode();
void Menu();
void main()
{
Menu();
getch();
}
void Menu()
{
char get;int n=1;
do{
printf("\n-------------------Menu--------------------\n");
printf("Addnode Enter 1\n");
printf("Deletenode Enter 2\n");
printf("Insertnode Enter 3\n");
printf("Shownode Enter 4\n");
printf("Exit Program Enter Q\n");
printf("--------------------------------------------\n");
printf("Enter your select\n");
get=getch();
switch(get)
{
case '1':system("cls");
Addnode();
break;
case '2':system("cls");
Delnode();
break;
case '3':system("cls");
Insnode();
break;
case '4':system("cls");
Shownode();
break;
case 'q':system("cls");
printf("Thank you");
n=0;
break;
default:system("cls");
}
}while(n!=0);
}
void Addnode()
{
struct node **walk=&start;
while(*walk!=NULL)
{
walk=&(*walk)->next;
}
*walk=new struct node;
printf("Input data:");
scanf("%s",(*walk)->data);
(*walk)->next=NULL;
}
void Shownode()
{
struct node **walk=&start;int n=1;
printf("---------------Show node-------------------\n");
if(*walk==NULL)
printf("Node is NULL");
else
{
while(*walk!=NULL)
{
printf("Node[%d]=%s\n",n,(*walk)->data);
n++;
walk=&(*walk)->next;
}
}
}
void Delnode()
{
int n;struct node
**walk=&start,*temp;
if(*walk==NULL)
printf("Eror!! delete nodeis NULL");
else{
printf("Enter number node to delete:");
scanf("%d",&n);
for(int
i=1;i<n;i++)
{
walk=&(*walk)->next;
}
temp=(*walk)->next;
delete[] *walk;
*walk=temp;
}
}
void Insnode()
{
int n;struct node
**walk=&start,*temp;
printf("Enter number node to insert:");
scanf("%d",&n);
for(int
i=1;i<n;i++)
{
walk=&(*walk)->next;
}
temp=*walk;
*walk=new struct node;
printf("Input data:");
scanf("%s",(*walk)->data);
(*walk)->next=temp;
}
doubly linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
char data[5];
struct node *next;
struct node *prve;
}*start=NULL;
int count;
void Addnode();
void Shownode();
void Delnode();
void Insnode();
void Menu();
void main()
{
Menu();
getch();
}
void Menu()
{
char get;int n=1;
do{
printf("\n-------------------Menu--------------------\n");
printf("Addnode Enter 1\n");
printf("Deletenode Enter 2\n");
printf("Insertnode Enter 3\n");
printf("Shownode Enter 4\n");
printf("Exit Program Enter Q\n");
printf("--------------------------------------------\n");
printf("Enter your select\n");
get=getch();
switch(get)
{
case '1':system("cls");
Addnode();
break;
case '2':system("cls");
Delnode();
break;
case '3':system("cls");
Insnode();
break;
case '4':system("cls");
Shownode();
break;
case 'q':system("cls");
printf("Thank you");
n=0;
break;
default:system("cls");
}
}while(n!=0);
}
void Addnode()
{
struct node **walk=&start,*temp;
if(*walk==NULL)
{
*walk=new struct node;
printf("Input data:");
scanf("%s",(*walk)->data);
(*walk)->prve=NULL;
(*walk)->next=NULL;
}
else
{
while(*walk!=NULL)
{
temp=*walk;
walk=&(*walk)->next;
}
*walk=new struct node;
printf("Input data:");
scanf("%s",(*walk)->data);
(*walk)->prve=temp;
(*walk)->next=NULL;
}
}
void Shownode()
{
struct node **walk=&start;int c=0;
printf("---------------Show now
node-------------------\n");
if(*walk==NULL)
printf("Node is NULL\n");
else
{
while(*walk!=NULL)
{
printf("***Adress prve=%x***\n",(*walk)->prve);
printf("Node[%d]=%s Adress is %x\n",c+1,(*walk)->data,*walk);
printf("***Adress next=%x***\n",(*walk)->next);
printf("----------------------------------------\n");
c++;
walk=&(*walk)->next;
}
}
count=c;
}
void Delnode()
{
int n,c=1;struct node
**walk=&start,*temp1,*temp2;
char get;
Shownode();
if(*walk==NULL)
printf("\nError!! delete nodeis NULL");
else
{
printf("Go to node:");
scanf("%d",&n);
if(n>count)
{
printf("Error!!\n");
c=0;
}
else
{
printf("Delete node[%d] enter A\n",n-1);
printf("Delete node[%d] enter B\n",n);
printf("Delete node[%d] enter C\n",n+1);
printf("Enter your select:");
get=getch();
switch(get)
{
case 'a':n=n-1;
break;
case 'A':n=n-1;
break;
case 'C':n=n+1;
break;
case 'c':n=n+1;
break;
case 'b':break;
case 'B':break;
default:printf("Error
!!");
c=0;
}
}
if(c!=0)
{
if(n==0||n>count)
{
printf("\nNode is NULL");
}
else
{
for(int
i=1;i<n;i++)
{
walk=&(*walk)->next;
}
temp1=(*walk)->prve;
temp2=(*walk)->next;
delete [] *walk;
*walk=temp2;
if(*walk!=NULL)
(*walk)->prve=temp1;
}
}
}
}
void Insnode()
{
int n,c=1;struct node
**walk=&start,*temp1=NULL,*temp2;
char get;
Shownode();
if(*walk==NULL)
{
printf("\nAuto Addnode");
n=1;
}
else
{
printf("Go to node :");
scanf("%d",&n);
if(n>count)
{
printf("Error!!\n");
c=0;
}
else
{
printf("Insert node[%d] enter A\n",n);
printf("Insert node[%d] enter B\n",n+1);
printf("Enter your select:");
get=getch();
switch(get)
{
case 'b': n=n+1;
break;
case 'B': n=n+1;
break;
case 'A': break;
case 'a': break;
default:printf("Error
choose !!");
c=0;
}
}
}
if(c!=0)
{
for(int
i=1;i<n;i++)
{
temp1=*walk;
walk=&(*walk)->next;
}
temp2=*walk;
*walk=new struct node;
printf("\nEnter data:");
scanf("%s",(*walk)->data);
(*walk)->prve=temp1;
(*walk)->next=temp2;
if(temp1!=NULL)
temp1->next=*walk;
if(temp2!=NULL)
temp2->prve=*walk;
}
}
#include<conio.h>
#include<stdlib.h>
struct node
{
char data[5];
struct node *next;
struct node *prve;
}*start=NULL;
int count;
void Addnode();
void Shownode();
void Delnode();
void Insnode();
void Menu();
void main()
{
Menu();
getch();
}
void Menu()
{
char get;int n=1;
do{
printf("\n-------------------Menu--------------------\n");
printf("Addnode Enter 1\n");
printf("Deletenode Enter 2\n");
printf("Insertnode Enter 3\n");
printf("Shownode Enter 4\n");
printf("Exit Program Enter Q\n");
printf("--------------------------------------------\n");
printf("Enter your select\n");
get=getch();
switch(get)
{
case '1':system("cls");
Addnode();
break;
case '2':system("cls");
Delnode();
break;
case '3':system("cls");
Insnode();
break;
case '4':system("cls");
Shownode();
break;
case 'q':system("cls");
printf("Thank you");
n=0;
break;
default:system("cls");
}
}while(n!=0);
}
void Addnode()
{
struct node **walk=&start,*temp;
if(*walk==NULL)
{
*walk=new struct node;
printf("Input data:");
scanf("%s",(*walk)->data);
(*walk)->prve=NULL;
(*walk)->next=NULL;
}
else
{
while(*walk!=NULL)
{
temp=*walk;
walk=&(*walk)->next;
}
*walk=new struct node;
printf("Input data:");
scanf("%s",(*walk)->data);
(*walk)->prve=temp;
(*walk)->next=NULL;
}
}
void Shownode()
{
struct node **walk=&start;int c=0;
printf("---------------Show now
node-------------------\n");
if(*walk==NULL)
printf("Node is NULL\n");
else
{
while(*walk!=NULL)
{
printf("***Adress prve=%x***\n",(*walk)->prve);
printf("Node[%d]=%s Adress is %x\n",c+1,(*walk)->data,*walk);
printf("***Adress next=%x***\n",(*walk)->next);
printf("----------------------------------------\n");
c++;
walk=&(*walk)->next;
}
}
count=c;
}
void Delnode()
{
int n,c=1;struct node
**walk=&start,*temp1,*temp2;
char get;
Shownode();
if(*walk==NULL)
printf("\nError!! delete nodeis NULL");
else
{
printf("Go to node:");
scanf("%d",&n);
if(n>count)
{
printf("Error!!\n");
c=0;
}
else
{
printf("Delete node[%d] enter A\n",n-1);
printf("Delete node[%d] enter B\n",n);
printf("Delete node[%d] enter C\n",n+1);
printf("Enter your select:");
get=getch();
switch(get)
{
case 'a':n=n-1;
break;
case 'A':n=n-1;
break;
case 'C':n=n+1;
break;
case 'c':n=n+1;
break;
case 'b':break;
case 'B':break;
default:printf("Error
!!");
c=0;
}
}
if(c!=0)
{
if(n==0||n>count)
{
printf("\nNode is NULL");
}
else
{
for(int
i=1;i<n;i++)
{
walk=&(*walk)->next;
}
temp1=(*walk)->prve;
temp2=(*walk)->next;
delete [] *walk;
*walk=temp2;
if(*walk!=NULL)
(*walk)->prve=temp1;
}
}
}
}
void Insnode()
{
int n,c=1;struct node
**walk=&start,*temp1=NULL,*temp2;
char get;
Shownode();
if(*walk==NULL)
{
printf("\nAuto Addnode");
n=1;
}
else
{
printf("Go to node :");
scanf("%d",&n);
if(n>count)
{
printf("Error!!\n");
c=0;
}
else
{
printf("Insert node[%d] enter A\n",n);
printf("Insert node[%d] enter B\n",n+1);
printf("Enter your select:");
get=getch();
switch(get)
{
case 'b': n=n+1;
break;
case 'B': n=n+1;
break;
case 'A': break;
case 'a': break;
default:printf("Error
choose !!");
c=0;
}
}
}
if(c!=0)
{
for(int
i=1;i<n;i++)
{
temp1=*walk;
walk=&(*walk)->next;
}
temp2=*walk;
*walk=new struct node;
printf("\nEnter data:");
scanf("%s",(*walk)->data);
(*walk)->prve=temp1;
(*walk)->next=temp2;
if(temp1!=NULL)
temp1->next=*walk;
if(temp2!=NULL)
temp2->prve=*walk;
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น