資料結構(C實現)------- 雙向鏈表,資料結構-------
雙向鏈表中的每一個結點都含有兩個指標域,一個指標域存放其後繼結點的儲存地址,另一個指標域則存放其前驅結點的儲存地址。
雙向鏈表結點的類型描述:
//雙向鏈表的類型描述typedef int ElemType;typedef struct node{ElemType data;struct node *prior,*next;}DuLNode,*DuLinkList;
其中,prior域存放的是其前驅結點的儲存地址,next域存放的是其後繼結點的儲存地址。
雙向鏈表有兩個特點:一是可以從兩個方向搜尋某個結點,這使得鏈表的某些操作(如插入和刪除)變得比較簡單; 二是無論利用前鏈還是後鏈都可以遍曆整個雙向鏈表。
雙向鏈表的操作基本和單鏈表的操作相同;
1. 頭插法建立帶頭結點的雙向鏈表Create_DLinkListF(int n)
//頭插法建立帶頭結點的雙向鏈表DuLinkList Create_DLinkListF(int n){DuLinkList L,p;int i = n - 1;ElemType x;//建立頭結點L = (DuLinkList)malloc(sizeof(DuLNode));L->prior = NULL;L->next = NULL;//添加第一個結點scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;L->next = p;p->prior = L;p->next = NULL;//加入其他結點while(i > 0){scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;p->next = L->next;L->next->prior = p;p->prior = L;L->next = p;i--;}return L;}
2. 尾插法建立帶頭結點的雙向鏈表Create_DLinkListR(int n)
//尾插法建立帶頭結點的雙向鏈表DuLinkList Create_DLinkListR(int n){DuLinkList L,p,lastNode;int i = n - 1;ElemType x;//建立頭結點L = (DuLinkList)malloc(sizeof(DuLNode));L->prior = NULL;L->next = NULL;//添加第一個結點scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;L->next = p;p->prior = L;p->next = NULL;lastNode = p;//加入其他結點while(i > 0){scanf("%d",&x);p = (DuLinkList)malloc(sizeof(DuLNode));p->data = x;lastNode->next = p;p->prior = lastNode;p->next = NULL;lastNode = p;i--;}return L;} 3. 在指定結點之前插入新結點Insert_DLinkListBefore(DuLinkList p,ElemType x)
//在指定結點之前插入新結點void Insert_DLinkListBefore(DuLinkList p,ElemType x){DuLinkList newNode;//判斷結點p之前的結點的合法性:if(p->prior == NULL)printf("結點不合法,不能在該結點之前插入結點\n");else{newNode = (DuLinkList)malloc(sizeof(DuLNode));newNode->data = x;newNode->next = p;p->prior->next = newNode;newNode->prior = p->prior;p->prior = newNode;}} 4. 在指定結點之後插入新結點Insert_DLinkListAfter(DuLinkList p,ElemType x)
//在指定結點之後插入新結點void Insert_DLinkListAfter(DuLinkList p,ElemType x){DuLinkList newNode;newNode = (DuLinkList)malloc(sizeof(DuLNode));newNode->data = x;//當插入位置是最後一個結點之後時if(p->next == NULL){p->next = newNode;newNode->prior = p;newNode->next = NULL;}else{newNode->next = p->next;p->next->prior = newNode;p->next = newNode;newNode->prior = p;}} 5. 刪除指定結點Delete_DLinkList(DuLinkList p)
//刪除指定結點void Delete_DLinkList(DuLinkList p){//如果刪除的是最後一個元素if(p->next == NULL)p->prior->next = NULL;else{p->prior->next = p->next;p->next->prior = p->prior;}free(p);} 6. 後鏈輸出雙向鏈表Print_DLinkListN(DuLinkList L)
//後鏈輸出雙向鏈表void Print_DLinkListN(DuLinkList p){while(p != NULL){printf("%d\t",p->data);p = p->next;}printf("\n");} 7.前鏈輸出雙向鏈表Print_DLinkListP(DuLinkList p)
//前鏈輸出雙向鏈表void Print_DLinkListP(DuLinkList p){while(p != NULL){printf("%d\t",p->data);p = p-prior;}printf("\n");}
至於雙向鏈表的其他動作,如定位,和單鏈表的操作類同,不再贅述。
用〈〈資料結構〉〉中的雙向鏈表作資料結構,結合C語言基本知識編寫一個通訊錄管理系統
#include <stdio.h>
#include <stdlib.h>
#define Null 0
#define OverFlow -1
#define OK 0
#define Error -2
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node *next;
}Node,*LinkList;
void Init_LinkList(LinkList *Head_pointer)
{//線性表初始化
*Head_pointer = Null;
}
int Insert_First(LinkList *Head_pointer,ElemType x)
{//採用頭插法建立線性表
Node *p;
p=(Node *)malloc(sizeof Node);
if(p==NULL)
return OverFlow;
p->data=x;
p->next = *Head_pointer;
*Head_pointer = p;
return OK;
}
LinkList Location_LinkList(LinkList Head,ElemType x)
{//查詢鏈表中某結點資料是否存在
LinkList p;
p=Head;
while(p!=Null)
{
if(p->data==x)
break;
p=p->next;
}
return p;
}
int Delete_LinkList(LinkList *Head_pointer,ElemType x)
{//刪除鏈表中的某一結點
Node *p,*q;
p=*Head_pointer;
if(p->data==x)//考慮頭結點就是要刪除的元素
{
*Head_pointer =(*Head_pointer)->next;
free(p);
return OK;
}
else
{
q=p;p=p->next;
while(p!=Null)
{
if(p->data==x)
{
q->next = p->next;
free(p);
return OK;
}
q=p;p=p->next;
}
}
return Error;
}
void Show_LinkList(LinkList Head)
{//遍曆線性表中的元素
LinkList p=Head;
int i=0;
printf("---鏈表列印---\n");
if(p==Null)
printf("空表\n");
while(p!=Null)
{
printf("[%d]:%d\t",i++,p->data);
p=p->next;
}
}
int Length_LinkList(LinkList Head)
{//求鏈表長度
LinkList p=Head;
int sum=0;
while(p!=Null)
{
sum++;
......餘下全文>>
用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......餘下全文>>