Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*........External Documentation........
- @@Linear Linked List
- Basic Implimentation with manue.
- Written By:
- Name: Md. Tarikul Islam.
- ID NO# 011 151 013
- United International University
- .......................................*/
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- struct list {
- int data;
- struct list *next;
- };
- typedef struct list node;
- // All the prototypes of functions
- void display (node *head);
- void insertatbegin (node *head);
- void insertatmiddle (node *head);
- void insertatlast (node *head);
- int sum (node *head);
- int maximum (node *head);
- int minimum (node *head);
- void linearsearch (node *head);
- void reverselist (node *head, int length);
- void bubble_sort(node *head, int length);
- // Main function Begins:
- int main(){
- int i, n;
- node *start, *prev, *temp;
- start = new node();
- start->next = NULL;
- prev = start; // making dummy node
- printf("How many data: ");
- scanf("%d", &n);
- printf("\nEnter %d data:\n\n", n);
- // taking n data to create linked list.
- for ( i = 0; i < n; i++) {
- temp = new node();
- scanf("%d", &temp->data);
- prev->next = temp;
- prev = temp;
- }
- printf("\nBefore Insertion: ");
- display(start);
- while (1) {
- printf("\n\tMANUE:\n\n");
- printf("\t1.Insert at the begin:\n\t2.Insert at the middle:\n\t3.Insert at the end:\n\t4.Display\n\t5.Exit:\n\t6.Sum:\n\t7.Maximum:\n\t8.Minimum\n\t9.Linear Search:\n\t10.Reverse List:\n\t11.Sort\n\n");
- int option;
- printf("\nOption_: ");
- scanf("%d", &option);
- switch(option){
- case 1:
- insertatbegin(start);
- break;
- case 2:
- insertatmiddle(start);
- break;
- case 3:
- insertatlast(start);
- break;
- case 4:
- display(start);
- break;
- case 5:
- exit(0);
- case 6:
- printf("\nSum is: %d\n", sum(start));
- break;
- case 7:
- printf("\nMaximum : %d\n", maximum(start));
- break;
- case 8:
- printf("\nMinimum: %d\n", minimum(start));
- break;
- case 9:
- linearsearch(start);
- break;
- case 10:
- reverselist(start, n);
- break;
- case 11:
- bubble_sort(start, n);
- break;
- default:
- break;
- }
- }
- return 0;
- // Main Function End.
- }
- // Function body Begins
- void display (node *head) {
- node *temp;
- temp = head->next;
- printf("\nDisplay: ");
- while (temp != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- void insertatbegin (node *head) {
- node *temp;
- temp = new node();
- printf("Insert new data: ");
- scanf("%d", &temp->data);
- temp->next = head->next;
- head->next = temp;
- }
- void insertatlast (node *head) {
- node *temp1, *temp;
- temp = new node();
- printf("Insert new data: ");
- scanf("%d", &temp->data);
- temp1 = head->next;
- while (temp1->next != NULL) {
- temp1 = temp1->next;
- }
- temp1->next = temp;
- temp->next = NULL;
- }
- void insertatmiddle (node *head) {
- node *temp1, *temp;
- temp1 = new node();
- printf("Insert new data: ");
- scanf("%d", &temp1->data);
- int n = temp1->data;
- temp = head;
- while ((temp->next != NULL) && (temp->next->data < n))
- temp = temp->next;
- temp1->next = temp->next;
- temp->next = temp1;
- }
- int sum (node *head) {
- node *temp;
- temp = head->next;
- int sum = 0;
- while (temp != NULL){
- sum = sum + temp->data;
- temp = temp->next;
- }
- return sum;
- }
- int maximum (node *head) {
- node *temp;
- temp = head->next;
- int mx = temp->data;
- while ( temp != NULL) {
- if (mx < temp->data)
- mx = temp->data;
- temp = temp->next;
- }
- return mx;
- }
- int minimum (node *head) {
- node *temp;
- temp = head->next;
- int mn = temp->data;
- while (temp != NULL) {
- if ( mn > temp->data)
- mn = temp->data;
- temp = temp->next;
- }
- return mn;
- }
- void linearsearch (node *head) {
- node *temp;
- temp = head->next;
- int key, flag;
- printf("key: ");
- scanf("%d", &key);
- while (temp != NULL) {
- if (key == temp->data)
- flag = 1;
- temp = temp->next;
- }
- if (flag == 1)
- printf("\nFound\n");
- else
- printf("\nNot Found\n");
- }
- void reverselist(node *head, int length) {
- node *start, *temp;
- start = NULL;
- for (int i = 1; i <= length; i++){
- temp = head->next;
- while (temp->next != start)
- temp = temp->next;
- printf("%d ", temp->data);
- start = temp;
- }
- printf("\n");
- }
- void bubble_sort(node *head, int length) {
- node *temp;
- int tp;
- for (int i = 1; i <= length; i++) {
- temp = head->next;
- while (temp->next != NULL){
- if ( (temp->data) > (temp->next->data)) {
- tp = temp->data;
- temp->data = temp->next->data;
- temp->next->data = tp;
- }
- temp = temp->next;
- }
- }
- display(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement