当前位置:首页 » 编程语言 » C语言双链表分页显示数据
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

C语言双链表分页显示数据

发布时间: 2022-11-26 15:05:07

① 用〈〈数据结构〉〉中的双向链表作数据结构,结合c语言基本知识。编写一个通讯录管理系统。

/*
目的:
用〈〈数据结构〉〉中的双向链表作数据结构,结合C语言基本知识。编写一个通讯录管理系统。
功能:
1)输入信息——enter();
2)显示信息———display();
3)查找以姓名作为关键字———search();
4)删除信息———delete();
5)存盘———save();
6)装入———load();
时间:
2017年7月20日09:50:14
版本:
1.0
环境:
gcc7.1
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#definemaxNameLength12
#definemaxAddrLength16
#definemaxPhoneLength12
#definemaxCacheLength64
///////////////////////////////////////////////////////////////////////////////////////
typedefstructperson
{
charname[maxNameLength];
charaddress[maxAddrLength];
charphone[maxPhoneLength];
structperson*prior,*next;
}singlePerson;
typedefstructaddressList
{
singlePerson*head;
intlength;
}addressList;
//////////////////////////////////////////////////////////////////////////////////////
voidinitialization(addressList&record);
intgetCommand(void);
voidhelp(void);
voiseradd(addressList&record);
voiddisplay(constaddressList&record);
voidshowSingle(singlePerson*elem);
voiserdel(addressList&record);
voidsearch(constaddressList&record);
voidsave(constaddressList&record);
voidload(addressList&record);
/////////////////////////////////////////////////////////////////////////////////////
intmain(void)
{
intengine=1;
addressListrecord;
initialization(record);
while(engine!=-1)
{
fflush(stdin);
printf("[entercommand]#");
engine=getCommand();
switch(engine)
{
case0:
help();
break;
case1:
useradd(record);
break;
case2:
display(record);
break;
case3:
search(record);
break;
case4:
userdel(record);
break;
case5:
save(record);
break;
case6:
load(record);
break;
case-1:
break;
case100:
break;
case500:
printf("NOsuchcommand ");
break;
default:
printf("Error ");
}
}
return0;
}
/////////////////////////////////////////////////////////////////////////////////////
voidinitialization(addressList&record)
{
record.head=NULL;
record.length=0;
}
intgetCommand(void)
{
char*cache=(char*)malloc(maxCacheLength*sizeof(char));
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=getchar();
}
*(cache+location)='';
strlwr(cache);
if(strcmp(cache,"help")==0)
{
free(cache);
return0;
}
elseif(strcmp(cache,"useradd")==0)//添加
{
free(cache);
return1;
}
elseif(strcmp(cache,"display")==0)//显示
{
free(cache);
return2;
}
elseif(strcmp(cache,"search")==0)//查找
{
free(cache);
return3;
}
elseif(strcmp(cache,"userdel")==0)//删除
{
free(cache);
return4;
}
elseif(strcmp(cache,"save")==0)//保存
{
free(cache);
return5;
}
elseif(strcmp(cache,"load")==0)//读取
{
free(cache);
return6;
}
elseif(strcmp(cache,"exit")==0)
{
free(cache);
return-1;
}
elseif(*cache=='')//NULL
{
free(cache);
return100;
}
else
{
free(cache);
return500;
}//default
}
voiseradd(addressList&record)
{
singlePerson*newNode=(singlePerson*)malloc(sizeof(singlePerson));
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(newNode->name+location++)=temp;
temp=getchar();
}
*(newNode->name+location)='',location=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(newNode->address+location++)=temp;
temp=getchar();
}
*(newNode->address+location)='',location=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(newNode->phone+location++)=temp;
temp=getchar();
}
*(newNode->phone+location)='',location=0;
newNode->next=record.head,newNode->prior=NULL;
if(record.head!=NULL)
record.head->prior=newNode;
record.head=newNode;
record.length++;
}
voiserdel(addressList&record)
{
char*cache=(char*)malloc(maxCacheLength*sizeof(char));
singlePerson*elem=record.head;
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=getchar();
}
*(cache+location)='';
strlwr(cache);
if(strcmp(cache,"all")==0)
{
if(elem==NULL)
printf("therearenocontactsinthelist ");
else
{
while(elem->next!=NULL)
{
elem=elem->next;
free(elem->prior);
}
free(elem);
record.head=NULL,record.length=0;
}
}
else
{
while(elem!=NULL)
{
if(strcmp(elem->name,cache)==0)
break;
elem=elem->next;
}
if(elem!=NULL)
{
if(elem==record.head)
{
record.head=elem->next;
}
else
{
if(elem->next!=NULL)
elem->next->prior=elem->prior;
elem->prior->next=elem->next;
}
free(elem);
}
else
printf("Thecontactcalled%swasnotfound ",cache);
}
free(cache);
}
voidsearch(constaddressList&record)
{
char*cache=(char*)malloc(maxCacheLength*sizeof(char));
singlePerson*elem=record.head;
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=getchar();
}
*(cache+location)='';
strlwr(cache);
while(elem!=NULL)
{
if(strcmp(elem->name,cache)==0)
break;
elem=elem->next;
}
if(elem==NULL)
printf("Thecontactcalled%swasnotfound ",cache);
else
showSingle(elem);
free(cache);
}
voidsave(constaddressList&record)
{
FILE*fileWrite;
char*cache=(char*)malloc(maxCacheLength*sizeof(char));
singlePerson*elem=record.head;
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=getchar();
}
*(cache+location)='';
strlwr(cache);
if((fileWrite=fopen(cache,"wt"))==NULL)
printf("Cant'tcreatethisfile ");
else
{
while(elem!=NULL)
{
fprintf(fileWrite,"%s%s%s ",elem->name,elem->address,elem->phone);
elem=elem->next;
}
fclose(fileWrite);
}
free(cache);
}
voidload(addressList&record)
{
FILE*fileRead;
char*cache=(char*)malloc(maxCacheLength*sizeof(char));
singlePerson*elem=NULL;
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=getchar();
}
*(cache+location)='';
strlwr(cache);
if((fileRead=fopen(cache,"rt"))==NULL)
printf("Cant'topenthisfile ");
else
{
temp=fgetc(fileRead);
while(feof(fileRead)==0)
{
elem=(singlePerson*)malloc(sizeof(singlePerson));
while(temp==''||temp==' ')
temp=fgetc(fileRead);
location=0;
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=fgetc(fileRead);
}
*(cache+location)='';
strlwr(cache);
strcpy(elem->name,cache);
while(temp==''||temp==' ')
temp=fgetc(fileRead);
location=0;
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=fgetc(fileRead);
}
*(cache+location)='';
strlwr(cache);
strcpy(elem->address,cache);
while(temp==''||temp==' ')
temp=fgetc(fileRead);
location=0;
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=fgetc(fileRead);
}
*(cache+location)='';
strlwr(cache);
strcpy(elem->phone,cache);
elem->next=record.head,elem->prior=NULL;
if(record.head!=NULL)
record.head->prior=elem;
record.head=elem;
record.length++;
temp=fgetc(fileRead);
}
fclose(fileRead);
}
free(cache);
}
voiddisplay(constaddressList&record)
{
char*cache=(char*)malloc(maxCacheLength*sizeof(char));
singlePerson*elem=record.head;
chartemp=getchar();
intlocation=0;
while(temp==''||temp==' ')
temp=getchar();
while(temp!=''&&temp!=' '&&temp!=' ')
{
*(cache+location++)=temp;
temp=getchar();
}
*(cache+location)='';
strlwr(cache);
if(strcmp(cache,"all")==0)
{
if(elem==NULL)
printf("therearenocontactsinthelist ");
else
{
while(elem!=NULL)
{
showSingle(elem);
elem=elem->next;
}
}
}
else
{
while(elem!=NULL)
{
if(strcmp(elem->name,cache)==0)
break;
elem=elem->next;
}
if(elem!=NULL)
showSingle(elem);
else
printf("Thecontactcalled%swasnotfound ",cache);
}
free(cache);
}
voidshowSingle(singlePerson*elem)
{
printf("name:%-10saddress:%-16sphone:%-12s ",elem->name,elem->address,elem->phone);
}
voidhelp(void)
{
printf("useradd: "
" 功能:添加一个联系人. "
" 格式:useraddnameaddressphone. "
" 例: ");
printf("display: "
" 功能:显示联系人. "
" 格式:displayname或displayall. "
" 例:displayxiaoming ");
printf("search: "
" 功能:查找联系人. "
" 格式:searchname. "
" 例:searchxiaoming ");
printf("userdel: "
" 功能:删除联系人. "
" 格式:useraddname或userdelall. "
" 例:userdelxiaoming ");
printf("save: "
" 功能:保存当前的通讯录. "
" 格式:savepath. "
" 例:saved:ackupaddressList.txt ");
printf("load: "
" 功能:读取通讯录. "
" 格式:loadpath. "
" 例:loadd:ackupaddressList.txt ");
}

② 用C语言编写一个通讯录系统,集合数据结构双向链表知识

//类似//#include "stdafx.h"
#include<iostream.h>
#include<string.h>

#include<iomanip.h>
class stu
{
char name[20];
double age,homephone,telphone;
char sex;
public:
stu(){}
stu(char n[20],char se,double ag,double ho,double te)
{
strcpy(name, n);
age=ag;
homephone=ho;
telphone=te;
}
friend void main();
}; void main()
{
cout<<"请选择您需要的操作!"<<endl;
cout<<"操作:"<<endl;
cout<<"(0)通讯录录入"<<endl;
cout<<"(1)增加人员"<<endl;
cout<<"(2)删除人员"<<endl;
cout<<"(3)修改数据"<<endl;
cout<<"(4)显示记录"<<endl;
cout<<"(5)退出"<<endl;
cout<<"选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl;
stu *s[50];
int i=0;
int j=0;
bool flag2=0;
char p;
do
{
cin>>p;
if((p>='0'&&p<='5'))
flag2=1;
else
cout<<"指令错误!请重新输入:"<<endl;
}while(flag2==0); switch(p)
{ case '0': //(0)通讯录录入
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{ cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别:"<<endl;
cin>>sex;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入家里的电话号码:"<<endl;
cin>>homephone;
cout<<"请输入移动电话号码:"<<endl;
cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break; }
////////////////////////////////////////////////////////////////////
case '1': //(1)增加人员(Add)
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{ cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别:"<<endl;
cin>>sex;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入家里的电话号码:"<<endl;
cin>>homephone;
cout<<"请输入移动电话号码:"<<endl;
cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break; } case '2': //(2)删除人员(Delete)
{
char name[20];bool flag3=0;char c;
do{
cout<<"请输入您要删除的学生姓名:"<<endl;
cin>>name;
for(int h=0;h<i;h++)
{
if(strcmp(name,s[h]->name)==0)
{
flag3=1;
i--;
do{
s[h]=s[h+1];
h++;
}while(h<=i);
}
}
if(flag3==0)
cout<<"您要求删除的对象本来就不存在!请检查输入的正确性!";
cout<<"要继续删除吗?(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break; }
case '3': //(3)修改数据(Alter)
{
char name[20],se;double ag,ho,te;flag2=0;
char c;
do
{
cout<<"请输入您要修改的学生的姓名:"<<endl;
cin>>name;
for(int h=0;h<i;h++)
{
if(strcmp(name,s[h]->name)==0)
{
flag2=1;
cout<<"请输入性别:"<<endl;
cin>>se;
cout<<"请输入年龄:"<<endl;
cin>>ag;
cout<<"请输入家里的电话号码:"<<endl;
cin>>ho;
cout<<"请输入移动电话号码:"<<endl;
cin>>te;
s[h]->sex=se;
s[h]->age=ag;
s[h]->homephone=ho;
s[h]->telphone=te;
cout<<"数据修改成功!";
}
}
if(flag2==0)
{
cout<<"您要修改的学生本来就不存在!请检查重新输入!"<<endl;
}
cout<<"想继续修改吗(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break; }
case '4': //(4)显示记录(List)
{
cout<<"本系统所有通讯录的数据如下:"<<endl;
if(i==0)
cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;
for(int k=0;k<i;k++)
{
cout<<k+1<<" "<<"姓名:"<<" "<<s[k]->name<<
"性别:"<<" "<<s[k]->sex<<"年龄:"<<" "<<s[k]->age
<<"家里的电话号码:"<<" "<<s[k]->homephone<<"移动电话号码:"
<<" "<<s[k]->telphone<<endl;
}
break; } }
cout<<"您想继续进行其他操作吗?(y/n)"<<endl;
bool flag4=0;
do
{
cin>>p;
if(p!='y'&&p!='n')
cout<<"指令错误!请重新输入!"<<endl;
else
flag4=1;
}while(flag4==0); if(p=='y')
cout<<"请输入操作代码(0 通讯录录入\n1 增加人员(Add)\n2 删除人员(Delete)\n3 修改数据(Alter)\n4 显示记录(List)\n 5 退出(Exit))"<<endl;
cin>>p;
for(int x=0;x<i;x++)
{
delete s[x];
cout<<"删除所有成员!"<<endl;
} }

③ C语言定义双向链表结构体

struct node
{
DataType data;
node * prior;
node * next;
};
其中prior指针用来存储前一节点的地址,next用来存储后一节点的地址,就比单项链表多了一个指针而已,可以添加其它自定义的数据成员

④ 使用C语言实现双向链表的建立、删除和插入

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct list{
int data;
struct list *next;
struct list *pre;
};
typedef struct list node;
typedef node *link;
link front=NULL,rear,ptr,head=NULL;

link push(int item){
link newnode=(link)malloc(sizeof(node));
newnode->data=item;
if(head==NULL)
{
head=newnode;
head->next=NULL;
head->pre=NULL;
rear=head;
}
else
{
rear->next=newnode;
newnode->pre=rear;
newnode->next=NULL;
rear=newnode;
}
return head;
}

void makenull(){
front=NULL;
rear=NULL;
}

empty(){
if(front==NULL)
return 1;
else
return 0;
}

int tops(){
if(empty())
return NULL;
else
return rear->data;
}

void pop(){
if(empty())
printf("stack is empty!\n");
else
rear=rear->pre;
}

void display(link l){
link p;
p=l;
while(p!=NULL){
printf("%d->",p->data);
p=p->next;
}
}

void main(){
int n,i;
printf("input your first data!\n");
scanf("%d",&n);
front=push(n);
/*another data*/
for(i=0;i<3;i++)
{
printf("input:\n");
scanf("%d",&n);
push(n);
}
ptr=front;
display(ptr);
printf("\n Please enter any key to pop");
pop();
ptr=front;
display(ptr);

}

⑤ c语言数据结构(双向链表排序)

#include<stdio.h>
#include<malloc.h>

#define ElemType int

int count=0;

typedef struct DulNode
{
ElemType data;
DulNode *prior;
DulNode *next;
}DulNode,*DulLinkList;

//初始化链表,结束后产生一个头结点指针
void InitDLList(DulLinkList *L)
{
(*L)=(DulLinkList)malloc(sizeof(DulNode));
(*L)->next=*L;
(*L)->prior=(*L)->next;
}
//对链表进行插入操作
void ListInsert(DulLinkList *L)
{
int i=0,n;
ElemType temp;
DulNode *s,*p;
p=(*L)->next;
printf("请输入插入元素数量:\n");
scanf("%d",&n);
count=n;
printf("请输入%d个自然数\n",n);
while(i<n)
{
scanf("%d",&temp);
s=(DulNode*)malloc(sizeof(DulNode));
s->data=temp;
while((p!=(*L))&&(p->data<temp))//查找所要插入的位置
{
p=p->next;
}

s->prior=p->prior;//新节点的插入
s->next=p;
p->prior->next=s;
p->prior=s;

p=(*L)->next;//将指针回指到链表第一个非空节点,主要是为了下次查找插入位置
i++;
}
}
void Display(DulLinkList L)
{
DulNode *p;
p=L->next;
printf("双向链表中的数据为:\n");
while(p!=L)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Sort(DulLinkList *L)
{
ElemType temp;
DulNode *p,*q;
p=(*L)->next;
q=(*L)->prior;
if(count%2!=0)
q=q->prior;
p=p->next;

while(p!=q)
{
temp=p->data;
p->data=q->data;
q->data=temp;

p=p->next;

if(p!=q) //第二题只需交换节点数据
q=q->prior;//这几个if else语句需要仔细
else
break;
if(p!=q)
p=p->next;
else
break;
if(p!=q)
q=q->prior;
else
break;
}

}
void main()
{
DulLinkList L;
InitDLList(&L);//初始化链表
ListInsert(&L);//顺序插入数据
Display(L);//显示结果
Sort(&L);//第二题操作
Display(L);//第二题输出结果
}

⑥ 双向链表排序c语言程序设计

/************************************************************************/
/*
文件名 doublelnk.h
作用 定义必要的结构体,并对双向链表的操作函数做声明
*/
/************************************************************************/

#ifndefDList_H
#defineDList_H
typedefintItem;
typedefstructNode*PNode;
typedefPNodePosition;
/*定义节点类型*/
typedefstructNode
{
Itemid; /*编号*/
Itemdata; /*数据域*/
PNodeprevious;/*指向前驱*/
PNodenext; /*指向后继*/
}Node;
/*定义链表类型*/
typedefstruct
{
PNodehead; /*指向头节点*/
PNodetail; /*指向尾节点*/
intsize;
}DList;

enumenumSortType
{
ID,
DATA
};

/*分配值为i的节点,并返回节点地址*/
PositionMakeNode(Itemid,Itemdata);

/*释放p所指的节点*/
voidFreeNode(PNodep);

/*构造一个空的双向链表*/
DList*InitList();

/*摧毁一个双向链表*/
voidDestroyList(DList*plist);

/*将一个链表置为空表,释放原链表节点空间*/
voidClearList(DList*plist);

/*返回头节点地址*/
PositionGetHead(DList*plist);

/*返回尾节点地址*/
PositionGetTail(DList*plist);

/*返回链表大小*/
intGetSize(DList*plist);

/*返回p的直接后继位置*/
PositionGetNext(Positionp);

/*返回p的直接前驱位置*/
PositionGetPrevious(Positionp);

/*将pnode所指节点插入第一个节点之前*/
PNodeInsFirst(DList*plist,PNodepnode);

/*将链表第一个节点删除并返回其地址*/
PNodeDelFirst(DList*plist);

/*获得节点的数据项*/
ItemGetItem(Positionp);

/*设置节点的数据项*/
voidSetItem(Positionp,Itemi);

/*删除链表中的尾节点并返回其地址,改变链表的尾指针指向新的尾节点*/
PNodeRemove(DList*plist);

/*在链表中p位置之前插入新节点S*/
PNodeInsBefore(DList*plist,Positionp,PNodes);

/*在链表中p位置之后插入新节点s*/
PNodeInsAfter(DList*plist,Positionp,PNodes);

/*返回在链表中第i个节点的位置*/
PNodeLocatePos(DList*plist,inti);

voidListTraverse(DList*plist,void(*visit)(Node));

/*对双向链表按照执行的排序方式进行排序*/
voidSortDLnk(DList*plist,enumSortTypesortType);

voidSortDLnkbyID(DList*plist);
voidSortDLnkbyData(DList*plist);
voidswapnode(PNodeanode,PNodebnode);
#endif


/************************************************************************/
/*
文件名 doublelnk.cpp
作用 对双向链表的操作函数进行具体实现
*/
/************************************************************************/
#include"doublelnk.h"
#include<malloc.h>
#include<stdlib.h>


/*分配值为i的节点,并返回节点地址*/
PositionMakeNode(Itemid,Itemdata)
{
PNodep=NULL;
p=(PNode)malloc(sizeof(Node));
if(p!=NULL)
{
p->id=id;
p->data=data;
p->previous=NULL;
p->next=NULL;
}
returnp;
}

/*释放p所指的节点*/
voidFreeNode(PNodep)
{
free(p);
}

/*构造一个空的双向链表*/
DList*InitList()
{
DList*plist=(DList*)malloc(sizeof(DList));
PNodehead=MakeNode(0,0);
if(plist!=NULL)
{
if(head!=NULL)
{
plist->head=head;
plist->tail=head;
plist->size=0;
}
else
returnNULL;
}
returnplist;
}

/*摧毁一个双向链表*/
voidDestroyList(DList*plist)
{
ClearList(plist);
free(GetHead(plist));
free(plist);
}

/*判断链表是否为空表*/
intIsEmpty(DList*plist)
{
if(GetSize(plist)==0&&GetTail(plist)==GetHead(plist))
return1;
else
return0;
}
/*将一个链表置为空表,释放原链表节点空间*/
voidClearList(DList*plist)
{
PNodetemp,p;
p=GetTail(plist);
while(!IsEmpty(plist))
{
temp=GetPrevious(p);
FreeNode(p);
p=temp;
plist->tail=temp;
plist->size--;
}
}

/*返回头节点地址*/
PositionGetHead(DList*plist)
{
returnplist->head;
}

/*返回尾节点地址*/
PositionGetTail(DList*plist)
{
returnplist->tail;
}

/*返回链表大小*/
intGetSize(DList*plist)
{
returnplist->size;
}

/*返回p的直接后继位置*/
PositionGetNext(Positionp)
{
returnp->next;
}

/*返回p的直接前驱位置*/
PositionGetPrevious(Positionp)
{
returnp->previous;
}

/*将pnode所指节点插入第一个节点之前*/
PNodeInsFirst(DList*plist,PNodepnode)
{
Positionhead=GetHead(plist);

if(IsEmpty(plist))
plist->tail=pnode;
plist->size++;

pnode->next=head->next;
pnode->previous=head;

if(head->next!=NULL)
head->next->previous=pnode;
head->next=pnode;

returnpnode;
}

/*将链表第一个节点删除,返回该节点的地址*/
PNodeDelFirst(DList*plist)
{
Positionhead=GetHead(plist);
Positionp=head->next;
if(p!=NULL)
{
if(p==GetTail(plist))
plist->tail=p->previous;
head->next=p->next;
head->next->previous=head;
plist->size--;

}
returnp;
}

/*获得节点的数据项*/
ItemGetItem(Positionp)
{
returnp->data;
}

/*设置节点的数据项*/
voidSetItem(Positionp,Itemi)
{
p->data=i;
}

/*删除链表中的尾节点并返回地址,改变链表的尾指针指向新的尾节点*/
PNodeRemove(DList*plist)
{
Positionp=NULL;
if(IsEmpty(plist))
returnNULL;
else
{
p=GetTail(plist);
p->previous->next=p->next;
plist->tail=p->previous;
plist->size--;
returnp;
}
}
/*在链表中p位置之前插入新节点s*/
PNodeInsBefore(DList*plist,Positionp,PNodes)
{
s->previous=p->previous;
s->next=p;
p->previous->next=s;
p->previous=s;

plist->size++;
returns;
}
/*在链表中p位置之后插入新节点s*/
PNodeInsAfter(DList*plist,Positionp,PNodes)
{
s->next=p->next;
s->previous=p;

if(p->next!=NULL)
p->next->previous=s;
p->next=s;

if(p=GetTail(plist))
plist->tail=s;

plist->size++;
returns;
}

/*返回在链表中第i个节点的位置*/
PNodeLocatePos(DList*plist,inti)
{
intcnt=0;
Positionp=GetHead(plist);
if(i>GetSize(plist)||i<1)
returnNULL;

while(++cnt<=i)
{
p=p->next;
}

returnp;
}

/*依次对链表中每个元素调用函数visit()*/
voidListTraverse(DList*plist,void(*visit)(Node))
{
Positionp=GetHead(plist);
if(IsEmpty(plist))
exit(0);
else
{

while(p->next!=NULL)
{
p=p->next;
visit(*p);
}
}
}


voidSortDLnk(DList*plist,enumSortTypesortType)
{
switch(sortType)
{
caseID:
SortDLnkbyID(plist);
break;
caseDATA:
SortDLnkbyData(plist);
break;
}
}

voidSortDLnkbyID(DList*plist)
{
PNodehead=plist->head;
Nodetmpnode;
Positioncurrnode=head;
Positionbianlinode;
while((currnode=currnode->next)!=NULL)
{
bianlinode=currnode;
while((bianlinode=bianlinode->next)!=NULL)
{
if(currnode->id>bianlinode->id)
{
swapnode(currnode,bianlinode);
}
}
}
}

voidSortDLnkbyData(DList*plist)
{
PNodehead=plist->head;
Nodetmpnode;
Positioncurrnode=head;
Positionbianlinode;
while((currnode=currnode->next)!=NULL)
{
bianlinode=currnode;
while((bianlinode=bianlinode->next)!=NULL)
{
if(currnode->data>bianlinode->data)
{
swapnode(currnode,bianlinode);
}
}
}
}

voidswapnode(PNodeanode,PNodebnode)
{//这里面要特别注意防止前驱节点是头结点和后驱节点是Null的情况
Nodetmpnode;
tmpnode.id=anode->id;
tmpnode.data=anode->data;

anode->id=bnode->id;
anode->data=bnode->data;

bnode->id=tmpnode.id;
bnode->data=tmpnode.data;
}/************************************************************************/
/*
文件名 program.cpp
作用 对双向链表的操作函数进行测试
*/
/************************************************************************/

#include"doublelnk.h"
#include<stdio.h>
voidprint(Nodenode)
{
printf("数据项:id=%d,data=%d ",node.id,node.data);
}
intmain()
{
DList*plist=NULL;
PNodep=NULL;

plist=InitList();
p=InsFirst(plist,MakeNode(12,23));
InsBefore(plist,p,MakeNode(2,54));
InsAfter(plist,p,MakeNode(3,34));

printf("p前驱位置的值为%d ",GetItem(GetPrevious(p)));
printf("p位置的值为%d ",GetItem(p));
printf("p后继位置的值为%d ",GetItem(GetNext(p)));


printf("遍历输出各节点数据项: ");
ListTraverse(plist,print);

printf("按照ID排序后遍历输出: ");
SortDLnk(plist,ID);
ListTraverse(plist,print);

printf("按照Data排序后遍历输出: ");
SortDLnk(plist,DATA);
ListTraverse(plist,print);

printf("除了头节点该链表共有%d个节点 ",GetSize(plist));
FreeNode(DelFirst(plist));
printf("删除第一个节点后重新遍历输出为: ");
ListTraverse(plist,print);
printf("除了头节点该链表共有%d个节点 ",GetSize(plist));

DestroyList(plist);
printf("链表已被销毁 ");

return0;
}

程序总共分三部分, 分别是双向链表的头文件、源文件和测试程序

建立工程后,分别建立相应的文件并添加相应代码应该就可以

下面的图片是我的运行效果(声明:程序中很多代码参考了以为前辈的代码http://blog.csdn.net/hopeyouknow/article/details/6716177)


⑦ 怎么使用C语言创建双层链表

双层链表是什么?数组链表还是双链表,如果是后者,使用尾查发的时候多一行代码就OK,就是指向前面一个节点。

1 #include<stdio.h>
2 #include<malloc.h>
3 struct nodeone
4 {
5 int data;
6 struct nodeone *next;
7 struct nodeone *down;
8 };
9
10 struct nodetwo
11 {
12 int data;
13 struct nodetwo *two;
14 };
15 void main()
16 {
17 int i = 0, j = 0;
18 struct nodeone *head, *p1, *p2, *p3;
19 p2 = head = (struct nodeone*)malloc(sizeof(struct nodeone));
20 while(i < 10)
21 {
22 p1 = (struct nodeone*)malloc(sizeof(struct nodeone));
23 p1->down = NULL;
24 while(j < 5)
25 {
26 p3 = (struct nodetwo*)malloc(sizeof(struct nodetwo));
27 p3->next = p1->down;
28 p1->down = p3;
j++;

29 }
30 p2->next = p1;
31 p2 = p1;
i++; //忘记了这两个计数变量,i和j。
32 }
33 p2->next = NULL;
34 }
~
理论上是对的,Linux下编译通过。外层使用尾插,内层使用头插,主要是为了减少一个变量。
我以前没有听过双层链表,不过记住这些名字还是有点用处。

⑧ C语言、双向链表

#include <stdio.h>
#include <string.h>
struct person
{
char name[10];
int number;
person *next;
person *last;
};person *p,*q,*start=NULL,*end;void insert(int num,char nam[10])//插入函数,按姓名的首字母顺序插入链表中的。
{
q=new person;
if (start==NULL) //判断start是否为空 若空则新建
{
start=q;
end=q;
p=q;
start->last=NULL;
end->next=NULL;
}
else
{

if(strcmp(nam,start->name)<=0)//插入链表头
{
start->last=q;
q->next=start;
q->last=NULL;
start=q;
}
else if (strcmp(nam,end->name)>0) //插入表尾部
{
end->next=q;
q->last=end;
end=q;
q->next=NULL;
}
else if (strcmp(nam,end->name)<0&&strcmp(nam,start->name)>0)//插入链表中
{
for (p=start;strcmp(nam,p->name)>0;p=p->next);
q->next=p;
p->last=q;
for (p=start;p->next!=q->next;p=p->next);
p->next=q;
q->last=p;
}

}
strcpy(q->name,nam);
q->number=num;

}
void find(int num) //按编号查找
{
if(start==NULL)
{
printf("无记录\n");
}
else
{
for(p=start;p!=NULL&&p->number!=num;p=p->next);
if(p==NULL)
{
printf("不存在的编号!\n");

}
else if (p->number==num)
{
printf("您查找的编号是:%d\n",num);
printf("该生的姓名为:%s",p->name);
}

}
} void del(int num) //按编号删除
{
for(p=start;p->number!=num;p=p->next);
if (p->number==num)
{
if (p->next==NULL)
{
if(p->last==NULL)
{
start=NULL;

}
else
{
(p->last)->next=NULL;
}
delete p;
}
else if (p->last==NULL)
{
(p->next)->last=NULL;
start = p->next;
delete p;
}
else if(p->last!=NULL&&p->next!=NULL)
{
(p->last)->next=p->next;
(p->next)->last=p->last;
delete p;
}
}

else
{
printf("不存在的编号!\n");
}

}void show()
{
printf("学号\t姓名\n");
for (p=start;;p=p->next)
{
printf("%d\t%s\n",p->number,p->name);
if (p->next==NULL)
break;
}
}void main()
{
int i,num;
char nam[10];
printf("输入三位学生信息(学号,姓名):");
for(i=0;i<3;i++)
{
scanf("%d%s",&num,nam);
insert(num,nam);
}
show();

printf("输入要删除的学生的学号:");
scanf("%d",&num);
del(num);
show();
printf("输入要查找的学生的学号:");
scanf("%d",&num);
find(num);
//show();
}

⑨ C语言关于双链表的一个问题:下面是代码:

根据你的程序,我做了一个小程序进行测试,算法没什么错误。不过要考虑
p1=first,如果first=NULL,那么p2=p1->next将是错误的,应该先判断下first
if(first==NULL) 直接将New插入
else {
开始查找插入点
}

下面是小程序
#include <stdio.h>
#include <stdlib.h>

typedef struct length
{
int age;
struct length *previous;
struct length *next;
}LENGTH;
LENGTH *ListAdd(LENGTH *New,LENGTH *first)
{
LENGTH *p1=NULL,*p2=NULL;
/*New=(LENGTH *)malloc(sizeof(LENGTH));*/
for(p1=first,p2=p1->next;p2!=NULL;p1=p1->next,p2=p2->next)
{
if(p2->age > New->age)
{
/*p2=p1->next;*/
p1->next=New;
New->previous=p1;
New->next=p2;
p2->previous=New;//应该是这里插入链表出了问题
break;
}
}
return first;
}

void main()
{
LENGTH *head,*p,*q,*New;

head=(LENGTH *)malloc(sizeof(LENGTH));
head->age=10;

p=(LENGTH *)malloc(sizeof(LENGTH));
p->age=20;
head->next=p;
p->previous=head;

q=(LENGTH *)malloc(sizeof(LENGTH));
q->age=40;
p->next=q;
q->previous=p;
q->next=NULL;

New=(LENGTH *)malloc(sizeof(LENGTH));
New->age=30;

head=ListAdd(New,head);
p=head;
while(p!=NULL)
{ printf("%d->",p->age);
p=p->next;
}

p=head;
while(p!=NULL)
{head=head->next;free(p);p=head;}
}

⑩ C语言双向链表

#include "stdio.h"
#include "stdlib.h"
typedef int ElemType;//元素类型
typedef struct DuLNode
{//双向链表
ElemType data;
struct DuLNode *prior, *next;
}DuLNode,*DuLinkList;
int Create(DuLinkList &L)
{//建立双向链表
DuLinkList p,q;
ElemType n,i;
L = (DuLinkList) malloc (sizeof(DuLNode));
L->next = NULL;
q = L;
printf("输入数据个数:");
scanf("%d",&n);
printf("输入数据元素:");
for ( i = 0; i < n; i++)
{
p = (DuLinkList) malloc (sizeof(DuLNode));
scanf("%d",&p->data);//插入数据
p->prior = q;
q->next = p;
p->next = 0;
q = q->next;
}
return 1;
}
int Visit(DuLinkList &L)
{//遍历双向链表
DuLinkList p;
p = L->next;
printf("双向链表为:");
while (p)
{
printf("%4d",p->data);
p = p->next;
}
printf("\n");
return 1;
}
int Clear(DuLinkList &L)
{
DuLinkList p;
p = L->next;
while(p)
{
L->next = p->next;
free(p);
p = L->next;
}
return 1;
}
main()
{
int i,e,s,num;
char c='y';
DuLinkList L;
Create(L);
Visit(L);
while (c=='y')
{
printf("\n\n\n1.双向链表插入元素\n2.双向链表中删除元素\n");
printf("3.判断双向链表元素是否对称\n");
printf("4.双向链表中奇数排在偶数前面\n");
printf("5.建立递增链表并有序插入元素\n\n");
printf("选择需要的操作\n\n");
scanf("%d",&num);
switch(num)
{
case 1:
printf("输入插入元素位置以及元素:\n");
scanf("%d%d",&i,&e);
ListInsert(L, i, e);
Visit(L);
break;
case 2:
printf("输入删除元素位置:");
scanf("%d",&i);
Delete(L,i,s);
printf("删除的元素为:%d\n",s);
Visit(L);
break;
case 3:
if(Same(L)) printf("链表对称\n");
else printf("链表不对称\n");
break;
case 5:
printf("清空原链表,建立递增链表:\n");
XCreate(L);
Visit(L);
break;
case 4:
printf("奇数放在偶数前面:\n");
Jiou(L);
Visit(L);
break;
}
printf("继续操作(y/n):\n");
scanf("%s",&c);
}
}