當前位置:首頁 » 編程語言 » 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);
}
}