最近在链表这里总是出问题,所以决定重新把链表给整一遍,好使自己不会在这个问题上在犯浑。。。
一个链表的增删改查。话不多说直接上代码:
界面的头文件:
#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
#include <stdio.h>
#include <stdio.h>
#include "Linklist.h"
int interface(link_list head)
{
int select=0;
while(select!=6)
{
printf("*************************\n");
printf("**1.Create linklist******\n");
printf("**2.Count linklist******\n");
printf("**3.Delete linklist******\n");
printf("**4.Insert linklist******\n");
printf("**5.Visit linklist******\n");
printf("**6. Exit ******\n");
printf("Do your choice : ");
scanf("%d",&select);
switch(select)
{
case 1: head=createlinklist(head);
break;
case 2: printf("The length of linklist is : %d\n",length_linklist(head));
break;
case 3: head=deletelinklist(head);
break;
case 4: head=insertlinklist(head);
break;
case 5: visitlinklist(head);
break;
case 6: return 0;
}
}
}
#endif // INTERFACE_H_INCLUDED
链表功能的头文件:
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct linklist{
char str[100];
struct linklist *next;
}linklist,*link_list;
/*
*Initialization the linklist.
*/
link_list initlinklist(link_list head)
{
head=(linklist *)malloc(sizeof(linklist));
memset(head->str,'\0',100);
head->next=NULL;
return head;
}
/*
*Creating the linklist.
*/
link_list createlinklist(link_list head)
{
head=initlinklist(head);
int n;
printf("How many node you want to create : ");
scanf("%d",&n);
link_list linkl1;
linkl1=initlinklist(linkl1);
printf("please input the string in linklist : ");
scanf("%s",linkl1->str);
head->next=linkl1;
int signal_end=1;
while(signal_end<n)
{
link_list linkl2;
linkl2=initlinklist(linkl2);
printf("please input the string in linklist : ");
scanf("%s",linkl2->str);
linkl1->next=linkl2;
linkl1=linkl2;
/*
*These code can make true the linklist inverted.
*At the same time you need to delete the point which named "linkl3".
*/
/*
****************************
*linkl2->next=linkl1->next;*
*linkl1->next=linkl2; *
****************************
*/
signal_end++;
}
return head;
}
/*
*The length of linklist.
*/
int length_linklist(link_list head)
{
link_list linkl;
linkl=head->next;
int length_linklist=0;
while(linkl!=NULL)
{
length_linklist++;
linkl=linkl->next;
}
return length_linklist;
}
/*
*Deleting the node in linklist.
*/
link_list deletelinklist(link_list head)
{
int destination=0;
char trash[100];
memset(trash,'\0',100);
if(length_linklist(head)==0)
{
return 0;
}
here : printf("please input the Node you want to delete : ");
scanf("%d",&destination);
link_list linkl1;
linkl1=initlinklist(linkl1);
linkl1=head;
int position=0; //point in linklist position
int len_linklist=0;
len_linklist=length_linklist(head);
if(destination>len_linklist-1)
{
printf("illegal position!\n");
goto here;
}
while(linkl1->next && position<destination-1)
{
linkl1=linkl1->next;
position+=1;
}
link_list linkl2;
linkl2=initlinklist(linkl2);
linkl2=linkl1->next;
linkl1->next=linkl2->next;
strcpy(trash,linkl2->str);
free(linkl2);
return head;
}
/*
*Insertiong the linklist
*/
link_list insertlinklist(link_list head)
{
link_list linkl1;
link_list linkl2;
linkl1=initlinklist(linkl1);
int destination=0;
int len_linklist=0;
int position=0; //point in linklist position
len_linklist=length_linklist(head);
there:printf("The total length is %d \nWhich destination you want to insert : ",length_linklist(head));
scanf("%d",&destination);
if(destination>len_linklist+1)
{
printf("Illegal\n");
goto there;
}
if(head==NULL) //If the linklist is NULL,then you can insert a head.
{
printf("please input the string you want to insert : ");
scanf("%s",linkl1->str);
head=linkl1;
linkl1->next=NULL;
}
else //If the linklist is not NULL,then you can choice the position you want to insert.
{
linkl1=head;
while(linkl1&&position<destination-1)
{
linkl1=linkl1->next;
position+=1;
}
if(!linkl1)
{
printf("illegal insert");
goto there;
}
linkl2=initlinklist(linkl2);
printf("please input the string you want to insert : ");
scanf("%s",linkl2->str);
linkl2->next=linkl1->next;
linkl1->next=linkl2;
}
return head;
}
/*
*Visting the linklist
*/
int visitlinklist(link_list head)
{
linklist *linkl;
linkl=head->next;
while(linkl!=NULL)
{
printf("%s\n",linkl->str);
linkl=linkl->next;
}
return 0;
}
#endif // LINKLIST_H_INCLUDED
最后就是主函数调用:
/*
************************************************
*Name : Linklist
*Date : 2014-10-08
*Author : marksman
*Aim : Creating the linklist.Achieving all opera
* -tions about the linklist.
************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Linklist.h"
#include "interface.h"
int main(int argc,char *argv[])
{
link_list linkl;
interface(linkl);
return 0;
}
代码在这望大家批评指正!