⑴ 如何用c語言編寫一個鏈表
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
struct Node
{
int data;//數據域
struct Node * next;//指針域
};
/*************************************************************************************
*函數名稱:Create
*函數功能:創建鏈表.
*輸入:各節點的data
*返回值:指針head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函數名稱:insert
*函數功能:在鏈表中插入元素.
*輸入:head 鏈表頭指針,p新元素插入位置,x 新元素中的數據域內容
*返回值:無
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函數名稱:del
*函數功能:刪除鏈表中的元素
*輸入:head 鏈表頭指針,p 被刪除元素位置
*返回值:被刪除元素中的數據域.如果刪除失敗返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函數名稱:print
*函數功能:列印鏈表中的元素
*輸入:head 鏈表頭指針
*返回值:無
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函數名稱:main
*函數功能:主函數創建鏈表並列印鏈表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}
⑵ 用C語言編輯鏈表,怎麼編輯呢
可能形式上有變化,單鏈表的本質是一樣的:
定義鏈表:
typedef struct linklist
{
Data;
struct linklist *next;
}linklist;
這是鏈表的基本單元,
鏈表都是由這些基本單元構成的
創建鏈表的方法很多,據了解學過的幾種循環都可以,在這給你演示一種:
#include<stdio.h>
#include<malloc.h>
typedef struct link
{
int num;
struct link *next;
}link;
link* creaetlink()
{
link *head ,*p,*q;
head = p = (link *)malloc(sizeof(link))
printf("請輸入數據:\n")
q = (link *)malloc(sizeof(link));
scanf("%d",&q->num);
while(num )
{
p-> next = q ;
p = q;
q = (link*)malloc(sizeof(link));
scanf("%d",&q->num);
}
free(q);
p->next = NULl;
return head;
}
這樣的資料很多,你有基礎,回去看一下熟悉一下,自己在電腦上敲兩遍就熟悉了
⑶ C語言 用鏈表形式編寫
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#define L sizeof(struct cargo)
struct cargo
{
long num;
char name[10];
float price;
int quantity;
struct cargo *next;
};
int n;
struct cargo *km()//輸入
{
struct cargo *head;
struct cargo *p1,*p2;
n=0;
p1=p2=(struct cargo *)malloc(L);
system("color 1");
printf("請輸入貨物的編碼,名字,價格,數量\n");
system("color 2");
scanf("%ld %s %f %d",&p1->num,&p1->name,&p1->price,&p1->quantity);
head=NULL;
while (p1->num!=0&&p1->next!=NULL)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct cargo*)malloc(L);
scanf("%ld %s %f %d",&p1->num,&p1->name,&p1->price,&p1->quantity);
}
p2->next=NULL;
return head;
}
void print(struct cargo *head)//查全部信息
{
struct cargo *p;
printf("尊敬的客戶您所查的全部信息共有%d件。\n",n);
system("color 7");
p=head;
if(head!=NULL)
{
printf("具體信息如下:\n");
printf(" ------------------------------------------------------------------------------\n");
printf(" | 商品編號 | 商品名稱 | 商品價格 | 商品數量 |\n");
printf(" | | | | |\n");
while (p!=NULL)
{
printf(" ---------------------------------------------------------------------\n");
printf(" | %ld | %s | %.2f | %d |\n",p->num,p->name,p->price,p->quantity);
p=p->next;
}
printf(" ---------------------------------------------------------------------\n");
}
else printf("這是一個空鏈表!\n");
}
void revise(struct cargo *head,long num)//修改
{
struct cargo *p;
p=head;
while (p!=NULL)
{
if(p->num==num)
{
printf("請重新輸入您需要修改的商品的編號,以示確定:\n");
system("color 5");
scanf("%ld",&p->num);
//sleep(100);
printf("請您把此商品的其他信息依次輸入:\n");
system("color 7");
scanf("%s %f %d",&p->name,&p->price,&p->quantity);
break;
}
p=p->next;
}
print(head);
}
delete(struct cargo *head,long num)//刪除
{
struct cargo *p1,*p2;
if(head==NULL)
{
printf("It's NULL!\n");
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head=p1->next;
free(p1);
}
else
{
p2->next=p1->next;
p1=NULL;
free(p1);
}
printf("刪除成功!\n");
n--;
}
else printf("沒有刪除的信息!\n");
print(head);
return head;
}
struct cargo*Insert(struct cargo*head,long num,struct cargo *nod)
{
struct cargo *p1;
if(head==NULL)
{
head=nod;//把內存給頭
nod->next=NULL;//只有一個信息則下一個為空
n+=1;
return head;
}
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p1=p1->next;
}
if(num==p1->num)
{
nod->next=p1->next;
p1->next=nod;
n+=1;
}
else
printf("沒有所要插入的節點!\n");
print(head);
return head;
}
struct cargo*find1(struct cargo *head,long num)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空鏈表!\n");
return head;
}
while(p->num!=num&&p->next!=NULL)
{
p=p->next;
}
if(num==p->num)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %.2f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
struct cargo*find2(struct cargo *head,char name[])
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空鏈表!\n");
return head;
}
while(p->name!=name&&p->next!=NULL)
{
p=p->next;
}
if(name==p->name)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %.2f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
struct cargo*find3(struct cargo *head,float price)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空鏈表!\n");
return head;
}
while(p->price!=price&&p->next!=NULL)
{
p=p->next;
}
if(price==p->price)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
struct cargo*find4(struct cargo *head,int quantity)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空鏈表!\n");
return head;
}
while(p->quantity!=quantity&&p->next!=NULL)
{
p=p->next;
}
if(quantity==p->quantity)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
{
struct cargo *head=NULL;
struct cargo *pre;
int a,myquantity;
char ch;
char name[20];
float myprice;
long thnum;
printf(" @_@歡迎進入倉庫管理系統@_@ \n");
printf("***************************************************************************\n");
printf("* *\n");
printf("* 歡 庫*\n");
printf("* *\n");
printf("* *\n");
printf("* 迎 管*\n");
printf("* *\n");;
printf("* *\n");
printf("* 進 輸入0代表輸入信息, 如果輸入四個0代表結束 理*\n");
printf("* *\n");
printf("* *\n");
printf("* 入 系*\n");
printf("* *\n");
printf("* *\n");
printf("* 倉 統*\n");
printf("* *\n");
printf("***************************************************************************\n");
system("color 5");
while(1)
{
printf("請輸入您需要的功能:\n");
printf("輸入1代表修改貨物信息\n");
printf("輸入2代表查看全部信息\n");
printf("輸入3代表刪除信息\n");
printf("輸入4代表插入一個新的商品\n");
printf("輸入5查找商品信息\n");
system("color 5");
printf("輸入6保存且退出系統!\n");
system("color 6");
printf("請輸入信息:\n");
printf("請做出選擇[ ]\b\b\b ");
scanf("%d",&a);
if(a==0)
{
head=km();
print(head);
}
if(a==2)
{
print(head);
}
if(a==1)
{
printf("請輸入您要修改的商品編號:\n");
scanf("%ld",&thnum);
revise(head,thnum);
}
if(a==3)
{
printf("請輸入您要刪除的商品編號:\n");
scanf("%ld",&thnum);
delete(head,thnum);
}
if(a==4)
{
printf("請輸入想要插入的位置:\n");
scanf("%ld",&thnum);
pre=(struct cargo*)malloc(L);
printf("輸入插入的信息:\n");
scanf("%ld %s %f %d", &pre->num,&pre->name,&pre->price,&pre->quantity);
Insert(head,thnum,pre);
}
if(a==5)
{
printf("請輸入查找方式:\n");
printf("@@@@@@輸入a代表用商品編號查找@@@@@@\n");
printf("@@@@@@輸入b代表用商品名字查找@@@@@@\n");
printf("@@@@@@輸入c代表用商品價格查找@@@@@@\n");
printf("@@@@@@輸入d代表用商品數量查找@@@@@@\n");
printf("請輸入信息:\n");
printf("請做出選擇[ ]\b\b\b ");
scanf("%c",&ch);
if(ch=='a')
{
printf("@@@@@@輸入商品編號進行查找@@@@@@\n");
scanf("%ld",&thnum);
find1(head,thnum);
}
else if(ch=='b')
{
printf("@@@@@@輸入商品名字進行查找@@@@@@\n");
scanf("%s",&name);
find2(head,name);
}
else if(ch=='c')
{
printf("@@@@@@輸入商品價格查找@@@@@@\n");
scanf("%f",&myprice);
find3(head,myprice);
}
else if(ch=='d')
{
printf("@@@@@@輸入商品數量查找@@@@@@\n");
scanf("%d",&myquantity);
find4(head,myquantity);
}
else
{
printf("選擇錯誤!!\n");
}
}
if(a==6)
{
printf("您已經退出倉庫管理系統,謝謝您的使用!\n");
break;
}
}
system("pause");
return 0;
}
⑷ 用C語言編寫一個鏈表
看完你下面的追問 其實 意思是
讓你把一個已有的 單鏈表
變成反向的單鏈表 對吧
⑸ 幫我編寫一個用C語言編寫的單鏈表的建立,和輸入輸出操作,謝謝各位
#include <stdio.h>
#include <windows.h>
typedef struct node
{
int num;
struct node *next;
}lnode;
lnode *creat()
{
lnode *head,*p,*q;
int n;
head=NULL;
printf("輸入要創建的節點數\n");
scanf("%d",&n);
while(n)
{
p=(lnode *)malloc(sizeof(lnode));
printf("輸入數據\n");
scanf("%d",&p->num);
if (head==NULL)
{
head=q=p;
}
else
{
q->next=p;
q=p;
}
n--;
}
q->next=NULL;
return head;
}
lnode *insert(lnode *head)
{
lnode *p,*q,*s;
int n;
char ch;
q=p=head;
printf("輸入插入的位置\n");
scanf("%d",&n);
printf("請選擇是插入在前還是在後(F or B)\n");
getchar();
ch=getchar();
if(ch=='F'||ch=='f')
{
s=(lnode *)malloc(sizeof(lnode));
printf("請輸入數據\n");
scanf("%d",&s->num);
while(p&&--n)
{
q=p;
p=p->next;
}
if (q==p)
{
s->next=q;
return s;
}
else
{
q->next=s;
s->next=p;
return head;
}
}
else if (ch=='B'||ch=='b')
{
s=(lnode *)malloc(sizeof(lnode));
printf("請輸入數據\n");
scanf("%d",&s->num);
while(p&&n--)
{
q=p;
p=p->next;
}
if (NULL==q->next)
{
q->next=s;
s->next=NULL;
return head;
}
else
{
q->next=s;
s->next=p;
return head;
}
}
else
{
printf("輸入錯誤\n");
}
}
lnode *del(lnode *head)
{
lnode *p,*q;
int n;
int flag=0;
p=q=head;
printf("請輸入刪除的數據\n");
scanf("%d",&n);
while(p)
{
if (p->num==n)
{
flag=1;
if (head==p)
{
head=head->next;
}
else if(NULL==p->next)
{
q->next=NULL;
}
else
{
q->next=p->next;
}
}
q=p;
p=p->next;
}
if (flag==0)
{
printf("沒有找到數據\n");
system("pause");
}
else
{
printf("刪除成功\n");
system("pause");
}
return head;
}
lnode *sort(lnode *head)
{
lnode *t,*f,*min,*p_min,*s;
char ch;
f=NULL;
printf("請輸入排序方式:升序(A/a),降序(D/d)\n");
getchar();
ch=getchar();
if (ch=='A'||ch=='a')
{
while(NULL!=head)
{
for (min=head,s=head;s->next!=NULL;s=s->next)
{
if (min->num>s->next->num)
{
p_min=s;
min=s->next;
}
}
if (NULL==f)
{
f=min;
t=min;
}
else
{
t->next=min;
t=min;
}
if (min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (f!=NULL)
{
t->next=NULL;
}
printf("排序完成\n");
system("pause");
head=f;
return f;
}
else if (ch=='D'||ch=='d')
{
while(NULL!=head)
{
for (min=head,s=head;s->next!=NULL;s=s->next)
{
if (min->num<s->next->num)
{
p_min=s;
min=s->next;
}
}
if (NULL==f)
{
f=min;
t=min;
}
else
{
t->next=min;
t=min;
}
if (min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (f!=NULL)
{
t->next=NULL;
}
printf("排序完成\n");
system("pause");
head=f;
return f;
}
}
void dispaly(lnode *head)
{
lnode *p;
p=head;
printf("\n");
while(p!=NULL)
{
printf("%d\t",p->num);
p=p->next;
}
}
int getoption()
{
int n;
printf("0 退出\n");
printf("1 創建鏈表\n");
printf("2 插入節點\n");
printf("3 刪除節點\n");
printf("4 排序節點\n");
printf("5 顯示鏈表\n");
printf("請選擇操作\t");
scanf("%d",&n);
return n;
}
int main()
{
lnode *temp;
char ch;
int o;
do
{
system("cls");
o=getoption();
switch (o)
{
case 0:
exit(0);
break;
case 1 :
system("cls");
temp=creat();
break;
case 2:
system("cls");
temp=insert(temp);
break;
case 3:
system("cls");
temp=del(temp);
break;
case 4:
system("cls");
temp=sort(temp);
break;
case 5:
system("cls");
dispaly(temp);
system("pause");
break;
}
system("cls");
printf("按0退出,任意鍵繼續\t");
ch=getchar();
if (ch=='0')
{
exit(0);
}
} while (ch!='0');
}
⑹ 用C語言編一個用鏈表的程序,求平均值的
如果鏈表的節點為:typedef struct node
{
double data;
struct node *next;
}Node; 則可以這樣寫: double avgfun(Node *h){ int n=0; double count=0; Node *p;
p = (Node *)malloc(sizeof(Node));
p = h;
while(p->next!=NULL)
{
p = p->next;
count += p->data; n++;
} return count/n;}
⑺ 求c語言鏈表編程
簡單寫了個!!看看運行一下
#include<iostream>
#include<conio.h>
using namespace std;
#define LENGTH 20
#define OK 1
#define ERROR 0
typedef long LElemType; /*學號變數類型*/
typedef int IElemType; /*成績變數類型*/
typedef char CElemType;
typedef int Status; /*返回值的類型*/
typedef struct student
{
LElemType num;
CElemType name[LENGTH];
CElemType sex[LENGTH];
IElemType age;
student *next;
}node;
typedef struct LIST
{
node *head,*tail;
}link;
Status Init(link *s)
{
s->head = s->tail = (node *)malloc(sizeof(node)); /*頭結點*/
if(NULL == s->head)
{
cout<<"分配失敗!"<<endl;
return ERROR;
}
s->head->num = 0; /*頭節點的學號用來放置節點個數*/
s->head->next = NULL;
return OK;
}
node *MakeNode(int i) /*生成存放學生信息的節點*/
{
node *p=(node *)malloc(sizeof(node));
cout<<"請輸入第"<<(i+1)<<"個學生的信息: ";
cin>>p->num>>p->name>>p->sex>>p->age;
p->next = NULL;
return p;
}
Status Insert(link *s,int n)
{
for(int i=0; i<n; i++) /*錄入n個學生的信息*/
{
node *p = MakeNode(i);
s->tail->next = p;
s->tail = p;
s->head->num++; /*修改頭結點中存放學生信息條數每增加一個節點就要++1*/
}
return OK;
}
Status display(link *s)
{
node *p = s->head->next; /*讓p指針指向頭結點的下一個節點,因為頭結點只用來存放了學生的人數*/
if(!p)
{
cout<<"還沒有學生信息!"<<endl;
return ERROR;
}
while(p)/*只要p指針指向不為空就要輸出*/
{
cout<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->age<<endl;
p = p->next;
}
return OK;
}
Status SortByNum(link *s) /*按照學號升序排序*/
{
int n = s->head->num;
node *p,*cur,*next;
for(int i=0; i<n-1; i++)
{
p= s->head;
cur=p->next;
next = cur->next;
for(int j=0; j<n-1-i; j++)
{
if(next->num<cur->num)
{
p->next=next; /*若前面的大於後面的就要交換前後兩個值*/
cur->next = next->next;
next->next = cur;
p=next; /*交換之後要修改當前指針與當前指針與下一個指針的位置*/
next=cur->next;
}else{ /*挨著的兩個值,前面的不大於後面的只要將指針往後面移動*/
p=cur;
cur = next;
next = next->next;
}
}
}
return OK;
}
Status DeleteStu(link *s,long *num)
{
if(s->head->next==NULL)
{
cout<<"沒有任何學生信息記錄"<<endl;
return ERROR;
}
else
{
cout<<"請輸入你要刪除的學生的學號:";
cin>>*num;
node *cur,*p;
cur = s->head;
while(cur->next)
{
if(cur->next->num == *num)
{
break;
}
else{
cur = cur->next;
}
}
if(cur->next)
{
p = cur->next;
cur->next = p->next;
p->next = NULL;
free(p);
s->head->num--;
return OK;
}else{
cout<<"不存在該學生的信息"<<endl;
return ERROR;
}
}
}
Status QueryStu(link *s,long num)
{
node *p = s->head->next; /*讓p指針指向頭結點的下一個節點,因為頭結點只用來存放了學生的人數*/
if(!p)
{
cout<<"還沒有學生信息!"<<endl;
return ERROR;
}
while(p)/*只要p指針指向不為空就要輸出*/
{
if(p->num==num)
{
cout<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->age<<endl;
break;
}
p = p->next;
}
cout<<"沒有該學生信息!"<<endl;
return OK;
}
Status Menu()
{
int k=-1;
while(k<1||k>5){
system("cls");
cout<< " 學生信息管理程序實現\n"
" 1、學生信息的錄入 \n"
" 2、學生信息的顯示 \n"
" 3、學生信息的查詢 \n"
" 4、學生信息的刪除 \n"
" 5、系統推出 \n\n\n";
if(k==-1)
{
cout<<"請選擇功能:";
}else if(k<1 || k>5)
{
cout<<"(選擇錯誤)請重新選擇:"<<endl;
}
fflush(stdin);
scanf("%d",&k);
}
return k;
}
int main()
{
link s;
int n=0,k=-1;
long num;
Init(&s);
do{
switch(k=Menu())
{
case 1:
cout<<"你想錄入多少學生的記錄:n=";
fflush(stdin);
scanf("%d",&n);
if(n!=0)
{
Insert(&s,n);
cout<<n<<"個學生信息錄入完畢...";
}
else{
cout<<"輸入學生個數要大於0的正整數...";
}
getch();
break;
case 2:
puts("所有學生的信息: \n");
SortByNum(&s);
display(&s);
cout<<"所有信息顯示完畢...";
getch();
break;
case 3:
cout<<"輸入要查詢學生的學號:";
cin>>num;
QueryStu(&s,num);
getch();
break;
case 4:
if(DeleteStu(&s,&num))
{
cout<<"刪除操作成功...";
}
else{
cout<<"刪除操作失敗...";
}
getch();
break;
case 5:
cout<<"成功退出了系統"<<endl;
exit(0);
break;
default:
break;
}
}while(k!=5);
return OK;
}
⑻ 50分求用c語言編寫鏈表程序
寫好了,你看下
#include
<stdio.h>
#include
<stdlib.h>
#include
<malloc.h>
typedef
struct
node
{
int
data;
struct
node
*next;
}Node;
void
InitList(Node
**head);
void
CreateList(Node
**head);
void
InsertList(Node
**head,
int
key);
void
DeleteList(Node
**head,
int
key);
void
PrintList(Node
**head);
//初始化鏈表
void
InitList(Node
**head)
{
(*head)
=
(Node
*)malloc(sizeof(Node));
(*head)->next
=
NULL;
}
//創建鏈表
void
CreateList(Node
**head)
{
int
i;
printf("您好,請輸入您要插入的數據:\n");
scanf("%d",
&i);
while(i
!=
0)
{
InsertList(head,
i);
scanf("%d",
&i);
}
}
//插入鏈表
void
InsertList(Node
**head,
int
key)
{
Node
*p,
*q,
*s;
q
=
(*head);
p
=
(*head)->next;
while(p)
{
q
=
p;
p
=
p->next;
}
s
=
(Node
*)malloc(sizeof(Node));
s->data
=
key;
s->next
=
NULL;
q->next
=
s;
}
//刪除鏈表
void
DeleteList(Node
**head,
int
key)
{
Node
*p,
*q;
q
=
(*head);
p
=
(*head)->next;
while(p
&&
p->data
!=
key)
{
q
=
p;
p
=
p->next;
}
if(p)
{
q->next
=
p->next;
free(p);
p
=
NULL;
}
}
//輸出鏈表
void
PrintList(Node
**head)
{
Node
*p;
p
=
(*head)->next;
while(p)
{
printf("%d\n",
p->data);
p
=
p->next;
}
}
int
main(void)
{
Node
*head;
int
i;
InitList(&head);
CreateList(&head);
printf("刪除前的數據:\n");
PrintList(&head);
printf("請輸入您要刪除的數據:\n");
scanf("%d",
&i);
DeleteList(&head,
i);
printf("刪除後的數據:\n");
PrintList(&head);
return
0;
}
Makefile:
#the
simplest
example
OBJS
=
tmp.o
CC
=
gcc
CFLAGS
=
-Wall
-O
-g
tmp:
$(OBJS)
$(CC)
$(OBJS)
-o
tmp
tmp.o:
tmp.c
$(CC)
$(CFLAGS)
-c
tmp.c
-o
tmp.o
clean:
rm
-f
*.o
*~
tmp
您好,請輸入您要插入的數據:
1
2
3
4
0
刪除前的數據:
1
2
3
4
請輸入您要刪除的數據:
1
刪除後的數據:
2
3
4
⑼ C語言編程關於鏈表
我有。。以下是程序的一部分。給我郵箱,我發給你吧~
/*鏈表的基本操作*/
# define NULL 0
# define ERROR 0
# define LEN sizeof(struct linklist)
struct linklist { /*鏈表的存儲結構的表示*/
int data;
struct linklist *next;
};
int n; /*定義n為全局變數*/
struct linklist *head;
struct linklist *create( ) /*創建一個空鏈表*/
{
struct linklist *p,*q; /* p、q 為指向struct linklist 類型數據的指針變數*/
p=q=(struct linklist*)malloc(LEN); /*開辟一個新單元*/
scanf ("%d",&p->data);
head=NULL;
while (p->data!=0) /*當所輸入數據不為0時,則執行循環體*/
{
n=n+1;
if (n==1) head=p;
else q->next=p;
q=p;
p=(struct linklist*)malloc(LEN); /*注要開辟了一個新單元後,才能輸入數據
scanf("%d",&p->data);
}
q->next=NULL;
return(head); /*返回鏈表的頭地址*/
}
⑽ 用C語言編這個鏈表
# 代表結束輸出
效果如下
m q i j#
l m y j#
m q i j
l m y j
j m intersection 交集
l m y j q i union of set 並集
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct linklist
{
ElemType data;
struct linklist *next;
}Node,*LinkList;
LinkList createFromTail(void)
{
LinkList head,tail;
char c;
Node *node;
head=tail=(LinkList)malloc(sizeof(Node));
head->next=NULL;
c=getchar();
while(c!='#')
{
node=(LinkList)malloc(sizeof(Node));
node->next=NULL;
node->data=c;
tail->next=node;
tail=node;
c=getchar();
}
return(head);
}
void print(LinkList h)
{
while(h->next!=NULL)
{
printf("%c ",h->next->data);
h=h->next;
}
}
LinkList insec(LinkList la,LinkList lb)
{
LinkList la_curr;
LinkList lb_curr;
LinkList insec_ab,temp;
la_curr=la->next;
insec_ab=(LinkList)malloc(sizeof(Node));
insec_ab->next=NULL;
while(la_curr!=NULL)
{
lb_curr=lb->next;
while(lb_curr!=NULL&&la_curr->data!=lb_curr->data)
{
lb_curr=lb_curr->next;
}
if(lb_curr!=NULL)
{
temp=(LinkList)malloc(sizeof(Node));
temp->data=la_curr->data;
temp->next=insec_ab->next;
insec_ab->next=temp;
}
la_curr=la_curr->next;
}
return(insec_ab);
}
LinkList mg(LinkList la,LinkList lb)
{
LinkList la_curr,la_tail;
LinkList lb_curr;
LinkList head,tail,temp;
la_curr=la->next;
head=tail=(LinkList)malloc(sizeof(Node));
head->next=NULL;
lb_curr=lb->next;
while(lb_curr!=NULL)
{
temp=(LinkList)malloc(sizeof(Node));
tail->next=temp;
temp->next=NULL;
tail=temp;
temp->data=lb_curr->data;
lb_curr=lb_curr->next;
}
while(la_curr!=NULL)
{
lb_curr=lb->next;
while(lb_curr!=NULL&&la_curr->data!=lb_curr->data)
{
lb_curr=lb_curr->next;
}
if(lb_curr==NULL)
{
temp=(LinkList)malloc(sizeof(Node));
tail->next=temp;
temp->next=NULL;
tail=temp;
temp->data=la_curr->data;
}
la_curr=la_curr->next;
}
return(head);
}
int main(void)
{
LinkList la,lb,lc;
printf("Please input character data for list la\n");
la=createFromTail();
printf("Please input character data for list lb\n");
lb=createFromTail();
printf("This is LinkList la\n");
print(la);
printf("\nThis is LinkList lb\n");
print(lb);
lc=insec(la,lb);
printf("\n Output the intersection\n");
print(lc);
printf("\n Output original linklist la\n");
print(la);
printf("\n Output original linklist lb\n");
print(lb);
lc=mg(la,lb);
printf("\n Output the union of set \n");
print(lc);
system("pause");
}